Giter VIP home page Giter VIP logo

pledge's Introduction

Pledge.js

Make a promise: build an ES6-style implementation

Javascript promises are versatile tools for managing asynchronous results. They are portable and can attach handler functions to an eventual value, in multiple places. Compared to the dead end of standard async callbacks, they restore normal control flow — letting you chain results, return new values, and catch errors where most convenient.

One way to understand a thing is to build it yourself. This repo contains a Jasmine 2.0 test spec (split into thematic chapters). Following the spec in order, we will build a constructor-style promise library similar to native ECMAScript Promises, which we will call pledge.js. Our promises will be named $Promise to avoid triggering browser code. To focus on concepts, pledge.js will use public variables and not be standards-compliant (see below).

Instructions

You'll need Node.js and its package manager npm installed.

npm install # automatically builds the docs and opens them
npm test

You will see all the upcoming tests as "pending" (yellow). Start writing your own code in the pledge.js file. When you pass a test (green), change the next pending test from xit to it and save. This spec is iterative and opinionated; it is recommended that you do the tests in order and not xit out any previous specs. For debugging, you can "focus" Jasmine specs/suites with fit/fdescribe.

Associated learning materials

The repo contains the lecture slides and a .then flowchart, both in PDF format.

The state of the art

There were once multiple proposed CommonJS promise standards, but one leading standard Promises/A+ and now a compliant ES6 implementation won out. However, many developers use Bluebird for its faster-than-native optimizations and many clever features.

Historically, there have been two ways to generate new promises: CommonJS-style deferreds, and simplified ES6-style constructors. We will study the ES6 style, which has emerged as both the official and de facto standard.

Warning

Legacy jQuery codebases beware! While jQuery 2 has a version of promises through $.Deferred, that implementation differed from current standards and is considered flawed. See Kris Kowal’s guide. However, modern jQuery users rejoice! jQuery 3 now features P/A+ compliant promises.

Technical note on non-compliance

Our pledge.js library is intended to be a learning exercise. Some of the Promises/A+ standards and general OOP principles that pledge.js will not cover include:

  • Handler functions should always be called in an async wrapper (e.g. setTimeout). This makes their behavior more deterministic as they execute after a following synchronous code line.
  • The .then() function should handle assimilation of promises from other libraries ("thenables"). That makes promises interoperable.
  • A promise's state and value should not be directly editable (public), only influenced or accessed through the resolver functions and .then().
  • For simplicity's sake, pledge.js does not always follow strict standards terminology. For example, it considers a pledge's value as meaning either its fulfillment data or rejection reason.

These and other technical details are important, but for someone just beginning to learn they distract from the core behavior and use patterns.

External Resources for Further Reading

Canon

General

Libraries

Angular and Related

pledge's People

Contributors

glebec avatar cigdemaybar avatar omriackley avatar halcarleton avatar

Watchers

James Cloos avatar  avatar

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.