Last active
July 30, 2018 18:50
-
-
Save jamesplease/b9ed94f225f0096dc9372d0220af6065 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
// 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