"It is easy to make promises - it is hard work to keep them."
-- Boris Johnson
This workshop is a starter-project with tests for implementing a fully-compliant Promise
constructor.
implement your Promise constructor in ./src/adapter.js and run:
npm install
npm test
To see fully implemented examples go to ./src/examples/ and/or run:
npm run test:native
npm run test:bluebird
- A
promise
is an object or function with a then method whose behavior conforms to the specification - A promise must be in one of three states: pending, fulfilled, or rejected.
- A promise must provide a
then
method which accepts two arguments:promise.then(onFulfilled, onRejected)
- For details, go to https://promisesaplus.com
-
The tests cover a lot of edge cases, don't feel obligated to make them pass in the order they appear
-
It might be easier to implement the
resolve
logic first, and defer implementing thereject
logic. They have a lot of similarities -
If
promise2 = promise1.then(onFulfilled, onRejected)
, implementations may allowpromise2 === promise1
, provided all requirements are met. I personally found it more convenient to implementpromise2 !== promise1
-
npm install lodash
-
Don't accidentally use the native
Promise
in your implementation
A promise represents the eventual result of an asynchronous operation.
The Promise
constructor was introduced in ECMAScript 6 (June 2015) and gave a native implementation to the Promise pattern. Prior to this, a number of JS libraries popped up, including the popular bluebird.js
(link)
-
Original proposal: http://wiki.commonjs.org/wiki/Promises/A
-
Extended Spec: https://promisesaplus.com / https://github.com/promises-aplus/promises-spec
-
Promises/A+ Compliance Test Suite: https://github.com/promises-aplus/promises-tests
Functional Programming aficionados might enjoy/wallow at why the "Promises/A+" specification does not to implement promises as monads: Twitter / Github issue