Skip to content

Instantly share code, notes, and snippets.

@tobius
Created March 14, 2019 21:27
Show Gist options
  • Save tobius/9e73555bdb66f762e18f94c52fdc848a to your computer and use it in GitHub Desktop.
Save tobius/9e73555bdb66f762e18f94c52fdc848a to your computer and use it in GitHub Desktop.
Promise.all Chaining Bug?
class Foo {
async run() {
setTimeout(() => {
console.log('long wait');
}, 5000);
this.start()
.then(() => {
console.log('promise.all block (counting)');
// @todo why aren't these promises called?
let promises = [];
for (let i in 3) {
promises.push(this.count.call(this, i));
}
return Promise.all(promises);
})
.then(() => {
console.log('promise inline block');
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('\tinline promise resolved');
resolve(true);
}, 1000);
});
})
.catch(this.fail.bind(this))
.finally(this.stop.bind(this));
}
async start() {
console.log('start');
}
async count(num) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('count', num);
resolve(true);
}, 1000);
});
}
async stop() {
console.log('stop');
}
async fail(err) {
console.error('fail', err);
process.exit(1);
}
}
// init
const foo = new Foo();
foo.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment