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
getUser("tom").then(function(user) { | |
getTweets(user).then(function(tweets) { | |
updateTimeline(tweets) | |
.then(function() { | |
console.log("done"); | |
}); | |
}); | |
}); |
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
getUser("tom") | |
.then(getTweets) | |
.then(updateTimeline) | |
.then(function(){ | |
console.log("done"); | |
}); |
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
fetchUser(username1) | |
.then(function(user1) { | |
fetchUser(username2) | |
.then(function(user2) { | |
makeFriends(user1, user2); | |
}); | |
}); |
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
var q = require("q"); | |
// v1 | |
q.all([getUser(username1), getUser(username2)]) | |
.then(function(users) { | |
makeFriends(users[0], users[1]); | |
}); | |
// v2 (even better) | |
q.all([fetchUser(username1), fetchUser(username2)]) |
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
var q = require("q"); | |
var getUser = function(username) { | |
return q.delay(500).then(function(){ | |
return username + " user"; | |
}); | |
}; | |
var getTweets = function(user) { | |
return q.delay(500).then(function(){ |
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
var getUserEmail = function(username) { | |
var deferred = q.defer(); | |
userRepository.find(username) // assume this returns a promise | |
.then(function(user){ | |
deferred.resolve(user.email); | |
}); | |
return deferred.promise; | |
}; | |
getUserEmail("emma").then(console.log); |
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
var getUserEmail = function (username) { | |
return userRepository.find(username).then(function (user) { | |
return user.email; | |
}); | |
}; | |
getUserEmail("emma").then(console.log); |
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
var Promise = require("bluebird"); | |
var apiUrls = ["url1", "url2", "url3"]; | |
var request = function(url) { | |
// request the rate limited api (returns a promise) | |
}; | |
var saveResponse = function(response) { | |
// save response to file/db (returns a promise) | |
}; |
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
var Promise = require("bluebird"); | |
var productIds = ["productId1", "productId2", "productId3"]; | |
var getProductsFromDb = function(productId) { | |
// returns a promise of product | |
} | |
var productPromises = productIds.map(getProductFromDb); | |
Promise.all(productPromises).then(function(products) { |
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
var Promise = require("bluebird"); | |
var fileParts = ["http://myfiles.com/file1/part1", | |
"http://myfiles.com/file1/part2", | |
"http://myfiles.com/file1/part3"]; | |
var fetchPart = function(partUrl) { | |
// returns the promise of the part of a file | |
}; | |
var filePartPromises = fileParts.map(fetchPart); |
OlderNewer