promise-waterfall-native
Version:
Small and simple function to use promises in waterfall
36 lines (32 loc) • 764 B
JavaScript
;
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);
});