Skip to content

Instantly share code, notes, and snippets.

@ashrafuzzaman
Created January 8, 2018 06:21
Show Gist options
  • Save ashrafuzzaman/6af06dcd2bfb38c4a3d00a4fdef691fc to your computer and use it in GitHub Desktop.
Save ashrafuzzaman/6af06dcd2bfb38c4a3d00a4fdef691fc to your computer and use it in GitHub Desktop.
Promise each
'use strict';
const Promise = require('bluebird');
let _updateResults = () => {
let promises = [];
for (let offset = 0; offset < 10; offset += 1) {
promises.push(new Promise(resolve => _updateResultsInChunk(offset).then(resolve)));
}
return Promise.each(promises, () => {
console.log('Done');
});
};
let _updateResultsInChunk = (offset) => {
console.log('Before Resolve ::' + offset);
return new Promise(resolve => {
console.log('1. Resolve ::' + offset);
resolve(offset);
}).then(() => new Promise(resolve => {
console.log('2. Resolve ::' + offset);
resolve(offset);
}));
};
_updateResults().then(() => {
process.exit(0);
}).catch((err) => {
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment