Created
March 14, 2019 21:27
-
-
Save tobius/9e73555bdb66f762e18f94c52fdc848a to your computer and use it in GitHub Desktop.
Promise.all Chaining Bug?
This file contains hidden or 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
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