Created
November 22, 2016 00:24
-
-
Save monkey-codes/4ab8ec54111295faeb7fe31844f28d68 to your computer and use it in GitHub Desktop.
rxjs - Example of Pull vs Push
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
//Traditional imperative pull based | |
window.setInterval(() => { | |
const tweets = getNewTweets(); | |
for(let tweet of tweets){ | |
if(!tweet.message.contains("Greetings!")) continue; | |
getUserAvatar(tweet.user) | |
.then(avatar => { | |
const $avatar = $(`<img />`); | |
$avatar.attr('src', avatar.url); | |
$avatars.append($avatar); | |
}); | |
} | |
}, 5000); | |
// Reactive push based | |
observeTweets() | |
.filter(s => s.message.contains('Greetings!')) | |
.flatMap(s => getUserAvatar(s.user)) | |
.map( a => $('< img/>').attr('src', a.url)) | |
.subscribe($avatar => { | |
$avatars.append($avatar); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment