"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
-
start by making
npm run test:basic
pass, see ./test/basic.test.js. -
The tests cover a lot of edge cases, don't feel obligated to make them pass in the order they appear
-
It can be helpful to run only a specific test instead of all 800+ of them. Go into
./node_modules/promises-aplus-tests/lib/tests
, find the relevant test file (e.g.2.1.3.js
) and add.only
to the specificdescribe
you want to narrow down to, e.g.describe("...
->describe.only("...
and rerunnpm run test
-
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