Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nsisodiya/chain-promise
Simple implementation for chaining promises !!
https://github.com/nsisodiya/chain-promise
Last synced: 27 days ago
JSON representation
Simple implementation for chaining promises !!
- Host: GitHub
- URL: https://github.com/nsisodiya/chain-promise
- Owner: nsisodiya
- License: mit
- Created: 2016-06-11T04:44:43.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-06-11T17:54:54.000Z (over 8 years ago)
- Last Synced: 2024-09-19T02:29:30.470Z (3 months ago)
- Language: JavaScript
- Size: 7.81 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# chain-promise
Simple implementation for chaining promises !!# Install
```bash
npm install chain-promise
```# Example
Let suppose we have 3 Async Process like this
```js
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.
```js
var D1 = {};
asyncProcess1(D1).then(function (D2) {
asyncProcess2(D2).then(function (D3) {
asyncProcess3(D3).then(function (D4) {
console.log("Final data is, ", D4);
});
});
});
```
Above code can be re-written as```js
asyncProcess1(D1)
.then(asyncProcess2)
.then(asyncProcess3)
.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 1 problem
* How to handle dynamic list of asyncProcessors ?
If you have dynamic list of then you can use this tiny library (source code is just 3-4 lines) !Above functionality can be re-written as
```js
var chainPromise = require("chain-promise");
chainPromise([asyncProcess1, asyncProcess2, asyncProcess3], D1).then(function (D4) {
console.log("Final data is, ", D4);
});
```