Skip to content

Background information #1

Open
Open
@domenic

Description

@domenic

This is rough and not spec-worthy, but is meant to give us a common starting point for future issues.

Terminology

  • "Unhandled rejection": the occurrence wherein a promise transitions to the rejected state, but no rejection handlers are registered for it.
  • "Handled later": if a promise is in the rejection state with no rejection handlers, but then one is added to the promise (via a then or catch call), we say that the rejection has now been "handled later." Notably, this could happen several seconds later.
  • "Crash": the behavior of a program when a synchronous exception is not handled. In Node.js and Windows 8 apps this is an actual crash; in browsers, this is an error to the console. "Crash" is just a shorthand.

Statement of the issue

  • With sync exceptions, if they get to the top of the stack with no catch blocks, then you can be sure that nobody will ever handle them, and the runtime knows your program should crash.
  • With async rejections, if they are unhandled, crashing is not the correct behavior, because they could be handled later.
  • The reason for this is that promises are first-class objects that can be passed around for handling in e.g. other program contexts or in response to other asynchronous events. But disallowing unhandled rejections (by crashing the moment they appear) essentially prohibits the use of promises in the rejected state as first-class objects.
  • But, since we can't react to unhandled rejections immediately by crashing, how should we react to them? And, if they are handled later, how should that impact the steps we took when they first appeared?

Sample Code

var promise = pendingPromise();

promise.then(function () {
    console.log("I only attached a handler for fulfillment");
});

rejectPromise(promise, new Error("who handles me?"));
// Nobody sees the error! Oh no, maybe we should crash here?

// But if we crashed there, then how would this code ever get run?
setTimeout(function () {
    promise.then(undefined, function (err) {
        console.error("I got it!", err);
    });
}, 5000);

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