Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the JavaScript ES6 promise exactly the same as Promise/A+?

I can find some blog posts stating that JavaScript ES6's promise is Promise/A+, but the official ES6 specs doesn't mention it, and MDN has a link to Promise/A+ at the very bottom, but didn't state that ES6 JavaScript's promise is exactly the same as Promise/A+.

Sometimes it is stated as "ES6 promise is based on Promise/A+", but are they exactly the same? ( is it mentioned in official docs, such as from ECMA or MDN?)

like image 648
nonopolarity Avatar asked Dec 27 '19 16:12

nonopolarity


1 Answers

They aren't identical, but JavaScript's promises are fully compliant with the Promises/A+ specification.

Promises/A+ is intentionally minimalist. JavaScript's promises have utility features that aren't covered by the Promises/A+ spec (such as catch and finally). Those are implemented using only features covered by Promises/A+, though. For instance, catch and finally are implemented via calls to then. (Literally, not just conceptually.) If you look at the specification for catch for example, it says catch is literally this:

class Promise {
    // ...

    catch(onRejected) {
        return this.then(undefined, onRejected);
    }

    // ...
}

(finally is a bit more complex, but it too ends up calling then and returning the promise it creates.)

JavaScript's promises also have the occasional optimization that only works if a native promise (instead of a promise from a third-party library) is used, but again, they're fully compliant with Promises/A+.

like image 103
T.J. Crowder Avatar answered Oct 04 '22 21:10

T.J. Crowder