Reference implementation of ECMA-262 Promises, in JavaScript.
This tries to stick as close as possible to the spec text but some things cannot be done with JavaScript...
When the spec wants us to create a function from some algorithm steps, instead we simply define an anonymous function with the steps.
Solution:
let alreadyResolved = false;
const resolve = (0, (resolution) => {
if (alreadyResolved) {
return;
}
// ...
});
The spec really wants JavaScript to be a language where people use monads instead of exceptions. Unfortunately we can't really recreate this. Instead, we simply pull the completion out via a try-catch block.
Solution:
// Let iteratorRecord be GetIterator(iterable).
// IfAbruptRejectPromise(iteratorRecord, promiseCapability).
// becomes:
let iteratorRecord;
try {
iteratorRecord = GetIterator(iterable);
} catch (e) {
iteratorRecord = e; // if iteratorRecord is an abrupt completion...
// IfAbruptRejectPromise(iteratorRecord, promiseCapability)
promiseCapability[kReject].call(undefined, iteratorRecord);
return promiseCapability[kPromise];
}