Skip to content

Reference implementation of ECMA-262 Promises, in JavaScript

Notifications You must be signed in to change notification settings

devsnek/promise-reference

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Promise Reference Implementation

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...

1. CreateBuiltinFunction

When the spec wants us to create a function from some algorithm steps, instead we simply define an anonymous function with the steps.

Example

Solution:

let alreadyResolved = false;
const resolve = (0, (resolution) => {
  if (alreadyResolved) {
    return;
  }

  // ...
});

2. Completions

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.

Example

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];
}

About

Reference implementation of ECMA-262 Promises, in JavaScript

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published