-
-
Save ruben1/63e99c9332644df86ff9 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
// Import node modules | |
var Q = require('q'); | |
var Promise = require('bluebird'); | |
var GitHubApi = require('github'); | |
// Instantiate github API. | |
// NOTE: This is just used for async demonstration purposes, you can imagine any other async functions through this example | |
var github = new GitHubApi({ | |
version: '3.0.0' | |
}); | |
/* * * * * A typical callback pattern * * * * * * */ | |
var getUserAvatarWithCallback = function(user, callback) { | |
// Use our node-github library to search for a specific user. This is async so we have to pass it a callback. | |
github.search.users({ q: user }, function(error, response) { | |
// Couldn't find a user for some reason. Pass along the error in consistent node format (error, response) | |
if (error) { callback(error, null); } | |
else { | |
var avatarUrl = response.items[0].avatar_url; // We know the specific property of interest from the github API docs | |
callback(null, avatarUrl); // Pass along our avatarUrl in consistent node format (error, response) | |
} | |
}); | |
}; | |
// Invoke our function, passing it a callback that will accept the avatar url as it's only parameter | |
getUserAvatarWithCallback('danthareja', function(error, url) { | |
console.log('Got url with callback pattern', url); | |
}); | |
/* * * * * Promises * * * * * * */ | |
var getUserAvatarWithBluebird = function(user) { | |
// Return a promise object using the 'new' keyword -> this is special to Bluebird's implementation | |
return new Promise(function(resolve, reject) { | |
github.search.users({ q: user }, function(error, response) { | |
// Whatever is passed into reject gets can be accessed in the 'catch' block | |
if (error) { reject(error); } | |
else { | |
var avatarUrl = response.items[0].avatar_url; | |
// Pass arguments of interest into resolve | |
// Whatever is passed into resolve gets can be accessed in the 'then' block | |
resolve(avatarUrl); | |
} | |
}); | |
}); | |
}; | |
// Invoke our 'promisified' function | |
getUserAvatarWithBluebird('danthareja') | |
.then(function(url) { | |
console.log('Got url with Bluebird promises', url); | |
}) | |
.catch(function(error) { | |
console.log('error getting avatar with q', error); | |
}); | |
var getUserAvatarWithQ = function(user) { | |
// Create a deferred object using Q's 'defer' constructor -> this is special to Q's implementation | |
var deferred = Q.defer(); | |
github.search.users({ q: user }, function(error, response) { | |
if (error) { deferred.reject(error); } // deferred.reject syntax is special to Q | |
else { | |
var avatarUrl = response.items[0].avatar_url; | |
deferred.resolve(avatarUrl); // deferred.resolve syntax is special to Q | |
} | |
}); | |
return deferred.promise; | |
}; | |
// Even though we use different syntax to create promises with bluebird and Q, the returned promise works exactly the same way | |
// i.e. they all have .then(), .catch(), .finally() | |
getUserAvatarWithQ('danthareja') | |
.then(function(url) { | |
console.log('Got url with q promises', url); | |
// Then's are chainable! Whatever we return in the previous then gets passed into the next one | |
return url + 'APPENDEDTEXT!!!!!! WOO'; | |
}) | |
.then(function(appendedAvatarURL) { | |
console.log('Check out the new appended url', appendedAvatarURL); | |
// We can return promises in the chain too! | |
// Our next then function will only run after the promise is resolved. | |
// Herein lies the true power of promsies, play around with this to learn more! | |
var deferred = Q.defer(); | |
setTimeout(function() { | |
deferred.resolve(appendedAvatarURL + 'AND WE DID MORE ASYNC'); | |
}, 1000); | |
return deferred.promise; | |
}) | |
.then(function(appendedAvatarURLAfterAnotherAsyncOperation) { | |
console.log('and we did more async! woah!', appendedAvatarURLAfterAnotherAsyncOperation); | |
}) | |
.catch(function(error) { | |
console.log('error getting avatar with q', error); | |
}) | |
// the 'finally' block will always get run regardless if the promise was resolved (successful) or rejected (errored) | |
.finally(function() { | |
console.log('this will always get run'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment