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

Exceptions swallowed by promises #17

Open

Description

@hgwood

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?

Activity

  1. kunalspathak commented on Jan 25, 2016

    @kunalspathak

    Thanks Hugo Wood (@hgwood) for reporting this. I will investigate this and get back to you.

  2. kunalspathak commented on Jan 25, 2016

    @kunalspathak

    Hugo Wood (@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
        }
     );
  3. hgwood commented on Jan 26, 2016

    @hgwood
    Author

    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?

  4. dotnetCarpenter commented on Feb 7, 2016

    @dotnetCarpenter

    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?

  5. dotnetCarpenter commented on Feb 7, 2016

    @dotnetCarpenter

    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
    });
  6. hgwood commented on Feb 8, 2016

    @hgwood
    Author

    Jon Ege Ronnenberg (@dotnetCarpenter) done is present in Bluebird, with the same semantics, but its usage is discouraged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions