Giter Club home page Giter Club logo

promises-workshop's Introduction

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 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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.