Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/devsnek/promise-reference
Reference implementation of ECMA-262 Promises, in JavaScript
https://github.com/devsnek/promise-reference
ecmascript promise reference-implementation
Last synced: about 1 month ago
JSON representation
Reference implementation of ECMA-262 Promises, in JavaScript
- Host: GitHub
- URL: https://github.com/devsnek/promise-reference
- Owner: devsnek
- Created: 2018-07-12T03:41:00.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-05-25T17:34:15.000Z (over 5 years ago)
- Last Synced: 2024-10-29T12:42:20.579Z (3 months ago)
- Topics: ecmascript, promise, reference-implementation
- Language: JavaScript
- Homepage:
- Size: 15.6 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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](https://tc39.github.io/ecma262/#sec-promise-resolve-functions)
Solution:
```js
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](https://tc39.github.io/ecma262/#sec-promise.all)
Solution:
```js
// 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];
}
```