Created
January 11, 2017 05:47
-
-
Save doron2402/480aaa9c82c862879c3b66305478cbda to your computer and use it in GitHub Desktop.
Running promises waterfall using promise-waterfall-native ('npm i promise-waterfall-native')
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const Waterfall = require('../index'); | |
const getSomeDataFromDB = () => { | |
return new Promise((resolve) => { | |
return setTimeout(() => { | |
resolve([ | |
{ id: 1 }, | |
{ id: 2 } | |
]); | |
}, 1000); | |
}); | |
}; | |
const makeHttpCall = (result) => { | |
const actions = result.map((obj) => { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
console.log(`making call using obj.id: ${obj.id}`); | |
const val = obj.id + 10; | |
return resolve(val); | |
}, 3000); | |
}); | |
}); | |
return Promise.all(actions); | |
}; | |
Waterfall([getSomeDataFromDB, makeHttpCall]) | |
.then((results) => { | |
// results should be an array with two items [11,12] | |
console.log(results); | |
}) | |
.catch((err) => { | |
console.log(err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment