Skip to content

Instantly share code, notes, and snippets.

@jamesplease
Last active July 30, 2018 18:50
Show Gist options
  • Save jamesplease/b9ed94f225f0096dc9372d0220af6065 to your computer and use it in GitHub Desktop.
Save jamesplease/b9ed94f225f0096dc9372d0220af6065 to your computer and use it in GitHub Desktop.
// The function
function subscribe(promises, cb) {
let result = Array.from({ length: promises.length }).fill(null);
promises.forEach((promise, index) => {
promise.then(val => {
const clonedResult = [...result];
clonedResult[index] = val;
cb(clonedResult);
result = clonedResult;
});
});
}
// Usage
subscribe([
new Promise(resolve => setTimeout(() => resolve(2), 2000)),
new Promise(resolve => setTimeout(() => resolve({ pizza: true }), 1000)),
], (stuff) => {
console.log('hi', stuff);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment