Giter Club home page Giter Club logo

chain-promise's Introduction

chain-promise

Simple implementation for chaining promises !!

Install

npm install chain-promise

Example

Let suppose we have 3 Async Process like this

var asyncProcess1 = function (obj) {
  return new Promise(function (resolve) {
    setTimeout(function () {
      console.log("inside asyncProcess1", obj);
      obj.a = "a";
      resolve(obj);
    }, 2000);
  })
};
var asyncProcess2 = function (obj) {
  return new Promise(function (resolve) {
    setTimeout(function () {
      console.log("inside asyncProcess2", obj);
      obj.b = obj.a + "b";
      resolve(obj);
    }, 2000);
  })
};
var asyncProcess3 = function (obj) {
  return new Promise(function (resolve) {
    setTimeout(function () {
      console.log("inside asyncProcess3", obj);
      obj.c = obj.b + "c";
      resolve(obj);
    }, 2000);
  })
};

And, We want to execute these Promises one by one. We want to pass data from one to another.

This is simple task, can be done like this.

var D1 = {};
asyncProcess1(D1).then(function (D2) {
  asyncProcess2(D2).then(function (D3) {
    asyncProcess3(D3).then(function (D4) {
      console.log("Final data is, ", D4);
    });
  });
});

You can see, we are passing data from each async process to another by one by one.

This works, But there are two problems

  • This kind of chain and passing data from one another can be automated !
  • If there are dynamic list of asyncProcessors then above solution will not work.

For this reason, I have created this tiny library.

Above functionality can be re-written as

var chainPromise = require("chain-promise");
chainPromise([asyncProcess1, asyncProcess2, asyncProcess3], D1).then(function (D4) {
  console.log("Final data is, ", D4);
});

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.