Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Stupid "Bitmain" question about resolving promises.
- //Run 3+ promises and check hoe many resolved and how many failed
- //Stats variable
- const stats = {success: 0, failed: 0};
- //Some promise
- const p = new Promise((resolve, reject) => {
- if(true) resolve();
- reject()
- });
- //Loop on set stats
- const pArr = [p,p,p];
- const run = () => {
- return new Promise(resolve => {
- for(let promise of pArr) {
- promise
- .then(() => {
- stats.success++;
- //Resolve the run after all promises resolves
- if(stats.success + stats.failed === pArr.length) resolve(stats);
- })
- .catch(() => {
- stats.failed++;
- //Resolve the run after all promises resolves
- if(stats.success + stats.failed === pArr.length) resolve(stats);
- })
- }
- });
- };
- run().then(console.log);
- //It's a stupid now because the question but because their answer includes Promise.all and
- //wrapping each promise in another "fake" promise that always resolve it.
Add Comment
Please, Sign In to add comment