Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

Exceptions swallowed by promises #17

Open
hgwood opened this issue Jan 23, 2016 · 6 comments
Open

Exceptions swallowed by promises #17

hgwood opened this issue Jan 23, 2016 · 6 comments

Comments

@hgwood
Copy link

hgwood commented Jan 23, 2016

Reprising your sample and introducing an obvious mistake shows that errors don't seem to escape promise handlers:

var uwp = require('uwp');
uwp.projectNamespace("Windows");

Windows.Storage.KnownFolders.documentsLibrary.createFileAsync(
  "sample.dat", Windows.Storage.CreationCollisionOption.replaceExisting)
  .done(
    function (file) {
      undefinedFunc();
    },
    function (error) {
      undefinedFunc();
    }
);

Running this shows no error, it just runs eternally because uwp.close() is never called. It makes it hard to detect errors, especially programming ones.

I read about WinJS.promise.onerror, but I'm not sure how it relates to the uwp module. Any pointers?

@kunalspathak
Copy link
Member

Thanks @hgwood for reporting this. I will investigate this and get back to you.

@kunalspathak
Copy link
Member

@hgwood , I would go with .then().done() approach as given below. I verified that this lets you catch the errors and close the uwp.

var uwp = require('uwp');
uwp.projectNamespace("Windows");

Windows.Storage.KnownFolders.documentsLibrary.createFileAsync(
  "sample.dat", Windows.Storage.CreationCollisionOption.replaceExisting)
  .then(
    function (file) {
      undefinedFunc();
    },
    function (error) {
      undefinedFunc();
    }
  ).done(
    function (result) {
    },
    function (error) {
      console.log('handle all the errors');
      uwp.close(); // close uwp
    }
 );

@hgwood
Copy link
Author

hgwood commented Jan 26, 2016

That's a nice work-around thanks, but wouldn't you say the original problem is a bug? Shouldn't done make its callback throw freely? Isn't that its main difference to then? I guess my real question is: could it be fixed in future versions?

@dotnetCarpenter
Copy link

Sorry if I add clutter to the conversation but it surprise me to see the .done() method in a Promise outside jQuery. There is no mention of a .done() method in the JS language specification.

Following the links from MSDN leads to a deprecated test suit for Promises/A.

AFAIK the list is:

Promise
Promise.all
promise.catch
Promise.prototype
Promise.race
Promise.reject
Promise.resolve
promise.then

Is the exact behavior of .done() documented anywhere?

@dotnetCarpenter
Copy link

Off the top of my head, I can only think of the following similar cases for JS Promises:

// Thenable throws before callback
// Promise rejects
var thenable = { then: function(resolve) {
  throw new TypeError("Throwing");
  resolve("Resolving");
}};
var p1 = Promise.resolve(thenable);
p1.then(function(v) {
  throw new Error("Should not be called"); // not called
}, function(e) {
  console.log(e); // TypeError: Throwing
});

// Thenable throws after callback
// Promise resolves
var thenable = { then: function(resolve) {
  resolve("Resolving");
  throw new TypeError("Throwing");
}};
var p2 = Promise.resolve(thenable);
p2.then(function(v) {
  console.log(v); // "Resolving"
}, function(e) {
  throw new Error("Should not be called"); // not called
});

@hgwood
Copy link
Author

hgwood commented Feb 8, 2016

@dotnetCarpenter done is present in Bluebird, with the same semantics, but its usage is discouraged.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants