Created
September 7, 2017 19:54
-
-
Save shairontoledo/0be90a2798039181e0c5f30ed109e3d7 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
const getToken = (id) => { | |
return new Promise((resolve) => { | |
setTimeout(() => { | |
console.log(`Resolved token for ${id}`) | |
resolve(`Token of id: ${id}`) | |
}, (Math.floor(Math.random() * 3000))) | |
}) | |
} | |
const getTokens = (ids) => { | |
return Rx.Observable.create((observer) => { | |
ids.forEach((id) => { | |
setTimeout(() => observer.next(`Token of id: ${id}`), 1000) | |
}) | |
setTimeout(() => { | |
console.log("Got tokens") | |
observer.complete() | |
}, 7000) | |
}) | |
} | |
const users = Rx.Observable.create((observer) => { | |
observer.next({ name: 'user1', id: 1 }) | |
setTimeout(() => observer.next({ name: 'user2', id: 2 }), 1000) | |
setTimeout(() => observer.next({ name: 'user3', id: 3 }), 2000) | |
setTimeout(() => observer.complete(), 4000) | |
}) | |
users | |
.map((user) => { | |
console.log('Got user: ' + JSON.stringify(user)) | |
return user.id | |
}) | |
.mergeMap(id => getToken(id)) | |
.toArray() | |
.subscribe(tokens => console.log('All tokens here: ' + JSON.stringify(tokens))) | |
// Expected output | |
// [ 'Token of id: 1', 'Token of id: 2', 'Token of id: 3' ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simulation: