GithubHelp home page GithubHelp logo

custom-promise's Introduction

Custom Promise

Implement a custom Promise in JavaScript

Reference Shubham Khatri

Usage

Installation

pnpm add @laqudee/custom-promise

examples

{
  "name": "custom-promise-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  //   "type": "module",  // if add type: module; test:cjs can't work
  "scripts": {
    "test": "node src/test.es.js",
    "test:es": "node src/test.es.mjs",
    "test:cjs": "node src/test.cjs.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@laqudee/custom-promise": "^1.0.0"
  }
}
// for cjs test.cjs.js
const CustomPromise = require('custom-promise')

// for es test.es.js with type: module(in package.json) or test.es.mjs
import CustomPromise from 'custom-promise'

Local Development

# build
pnpm build
# link fro global
pnpm link --global

# when use custom-promise
pnpm link --global @laqudee/custom-promise

# test
pnpm test:es
pnpm test:cjs

Promises/A+ Compliance Test Suite

This suite tests compliance of a promise implementation with the Promises/A+ specification.

Promises/A+ CTS

  • add promise-aplus-tests
pnpm add promises-aplus-tests -D
  • add script cammand
"scripts": {
  "test": "promises-aplus-tests promise-a-plus-test.js"
}
  • run test
pnpm test

This lib is temporarily uninstalled because it uses an older version of mocha that is causing some security issues. You can use this lib locally to test custom promises, but make sure it is uninstalled when you publish your own repository or npm package.

pnpm dev Instructions

  • pnpm dev will use node and esm execute the src/index.js file.

Implement Process

States, Handlers and value

  • States:

    • Pending
    • Fulfilled
    • Rejected
  • Handlers

    • then()
    • catch()
    • finally()
  • value

    • resolve() return value
    • reject() return reason

Start Coding

A Promise is executed as soon as it is created, so callback function will be called inside the constructor with reject and resolve methods passed as paramenters to it.

refer to: Building Promises from scratch

custom-promise's People

Contributors

laqudee avatar

Stargazers

 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.