Make You a Promise for Great Good!

"It is easy to make promises - it is hard work to keep them."

-- Boris Johnson

Workshop

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

Quick Refresher

  • 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

Notes, Tips and Recommendations

  • 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 the reject logic. They have a lot of similarities

  • If promise2 = promise1.then(onFulfilled, onRejected), implementations may allow promise2 === promise1, provided all requirements are met. I personally found it more convenient to implement promise2 !== promise1

  • npm install lodash

  • Don't accidentally use the native Promise in your implementation

Background

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)

FP Anecdote

Functional Programming aficionados might enjoy/wallow at why the "Promises/A+" specification does not to implement promises as monads: Twitter / Github issue