Created
March 16, 2021 10:42
-
-
Save rajatjain-21/63fb9d04aac47af651cb64acb25520bf to your computer and use it in GitHub Desktop.
This file contains 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 MyPromise { | |
static all(promises) { | |
let results = []; // to be resolved array of values | |
let completedPromises = 0; | |
// return a promise | |
return new Promise((resolve, reject) => { | |
promises.forEach((promise, index) => { | |
promise.then((value) => { | |
results[index] = value; | |
completedPromises += 1; | |
if(completedPromises === promises.length) { | |
// to break the loop | |
resolve(results); | |
} | |
}).catch(function (error) { | |
reject(error); | |
}); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment