Skip to content

Instantly share code, notes, and snippets.

@chinmay185
chinmay185 / promise-hell.js
Last active August 29, 2015 14:18
Writing promises in callback style (ugly)
getUser("tom").then(function(user) {
getTweets(user).then(function(tweets) {
updateTimeline(tweets)
.then(function() {
console.log("done");
});
});
});
@chinmay185
chinmay185 / promise-land.js
Last active August 29, 2015 14:19
Flattening the promise chain. Writing promises in idiomatic style.
getUser("tom")
.then(getTweets)
.then(updateTimeline)
.then(function(){
console.log("done");
});
@chinmay185
chinmay185 / nested-promises.js
Last active August 29, 2015 14:19
do task1, do task2 and when both are done, do something else
fetchUser(username1)
.then(function(user1) {
fetchUser(username2)
.then(function(user2) {
makeFriends(user1, user2);
});
});
@chinmay185
chinmay185 / straighten-promises.js
Last active August 29, 2015 14:19
using q.all and q.spread to straighten promises
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)])
@chinmay185
chinmay185 / missing-return.js
Created April 13, 2015 13:23
missing return breaks the promise chain and causes subtle bugs
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(){
@chinmay185
chinmay185 / deferred-abused.js
Last active August 29, 2015 14:19
unnecessary use of deferred.
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);
@chinmay185
chinmay185 / deferred-fixed.js
Last active August 29, 2015 14:19
getting rid of unnecessary deferred
var getUserEmail = function (username) {
return userRepository.find(username).then(function (user) {
return user.email;
});
};
getUserEmail("emma").then(console.log);
@chinmay185
chinmay185 / sequential-async.js
Last active August 29, 2015 14:19
Run async operation for each value in a collection where each async operation is executed only when the previous one completes.
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)
};
@chinmay185
chinmay185 / parallel-async.js
Created April 27, 2015 15:46
using map to start multiple async operations at the same time
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) {
@chinmay185
chinmay185 / async-map-reduce.js
Last active August 29, 2015 14:20
Demonstration of async map reduce using promises
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);