Skip to content

wix-academy/promises-workshop

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

  • 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 specific describe you want to narrow down to, e.g. describe("... -> describe.only("... and rerun npm run test

  • 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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%