Code Monkey home page Code Monkey logo

promise-aplus's Introduction

promise-aplus

A simple Promise/A+ implementation in TypeScript.

Description

Promise/A+ is a specification for asynchronous JavaScript promises. This project is a simple implementation of the specification in TypeScript. For more information on the specification, see https://promisesaplus.com/.

Flowchart

Running locally

To install dependencies:

bun install

To run:

bun run dev

To test:

bun run test

Takeaways

Promise can only have 3 status: pending, fulfilled, rejected.

  • When in pending, can transit to fulfilled or rejected.
  • When in fulfilled or rejected, can't transit to other status && must have value.
  • When in rejected, can't transit to fulfilled && must have reason.

Promise constructor

  • The executor function in constructor takes two arguments, resolve and reject, both are functions.
  • Constructor is called immediately when PromiseAPlus instance is created.

Most of the time executor is async, e.g. setTimeout, fetch, XMLHttpRequest, etc.

  1. Takes an executor function as argument, e.g. (resolve, reject) => { ... }
  2. Then call executor function by passing resolve and reject functions as arguments under the context of Promise instance, e.g. executor.call(this, resolve, reject)
  • As we pass resolve & reject to executor, this context is lost.

Be careful when passing a function as a callback to another function, the this context inside the callback is lost.

executor(this.resolve.bind(this), this.reject.bind(this));

then method

  • then method takes two arguments, onFulfilled and onRejected.
  • then method will be called when chaining then method on Promise instance.

resolve method

  • resolve will be called when executor function is called with resolve.
  • Basically it will be called when Promise is fulfilled (change status from pending to fulfilled).
  • Loop through onFulfilledCallbacks and call each callback with this.value as argument.

reject method

  • reject is similar to resolve, but it will only be called when Promise is rejected (change status from pending to rejected).

catch method

  • catch method is a simply then(undefined, onRejected).
  • catch method will be called when chaining catch method on Promise instance.

finally method

  • finally method is a simply then(onFinally, onFinally).
  • finally method will be called when chaining finally method on Promise instance no matter fulfilled or rejected

License

This project was created using bun init in bun v1.0.21. Bun is a fast all-in-one JavaScript runtime.

References

promise-aplus's People

Contributors

callyberz avatar

Watchers

 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.