Skip to content

Instantly share code, notes, and snippets.

@nervetattoo
Last active December 10, 2016 09:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nervetattoo/97692f4823c84afad3bf to your computer and use it in GitHub Desktop.
Save nervetattoo/97692f4823c84afad3bf to your computer and use it in GitHub Desktop.
promises-promises-promises
async.auto({
get_data: function(callback){
console.log('in get_data');
// async code to get some data
callback(null, 'data', 'converted to array');
},
make_folder: function(callback){
console.log('in make_folder');
// async code to create a directory to store a file in
// this is run at the same time as getting the data
callback(null, 'folder');
},
write_file: ['get_data', 'make_folder', function(callback, results){
console.log('in write_file', JSON.stringify(results));
// once there is some data and the directory exists,
// write the data to a file in the directory
callback(null, 'filename');
}],
email_link: ['write_file', function(callback, results){
console.log('in email_link', JSON.stringify(results));
// once the file is written let's email a link to it...
// results.write_file contains the filename returned by write_file.
callback(null, {'file':results.write_file, 'email':'user@example.com'});
}]
}, function(err, results) {
console.log('err = ', err);
console.log('results = ', results);
});
fs.readFile('data.json', function(err, data) {
if (err) // handle it
try {
var json = JSON.parse(data);
render(json);
}
catch (e) { // handle it }}
});
getUser(userId, function(err, user) {
if (err) handleError(err);
else {
getUserFriends(user, function(err, friends) {
if (err) handleError(err);
else {
getUserProfile(friends[0], function(err, json) {
if (err) handleError(err);
else {
try {
var bestFriend = JSON.parse(json);
refreshUi(bestFriend);
}
catch (Exception e) {
handleError(e.message);
}
}
});
}
});
}
});
var squareCb = function(num, cb) {
cb(null, num * num);
};
squareCb(2, function(err, num) {
squareCb(num, function(err, result) {
logIt(result);
});
});
var square = function(num) {
return Promise.resolve(num * num);
}
square(2).then(square).then(logIt);
var nestedPromises = function() {
return new Promise(function(resolve, reject) {
new Promise(function(resolve2, reject2) {
resolve2("Hello world");
}).then(resolve, reject)
})
}
nestedPromises().then(logIt);
getUser(userId)
.then(function(user) {
return getUserFriends(user);
})
.then(function(friends) {
return getUserProfile(friends[0]);
})
.then(JSON.parse)
.then(frefreshUi, handleError);
var square = function(num) {
return num * num;
}
Promise.resolve(square(2)).then(square).then(logIt);
Promise
.all([get_data, make_folder])
.spread(function write_file(data, folder) { return 'filename'; })
.then(function email_link(filename) {
return sendEmail({file: filename});
})
.catch(function(err) { console.log(err); });
logIt = console.log.bind(console)
var Promise = require('bluebird');
// Entire library
var fs = Promise.promisifyAll(require('fs'));
fs.readFile('foo.json').then(JSON.parse).then(console.log.bind(console));
// Method
var request = Promise.promisify(require('request'));
request('http//foo.com/foo.json').then(JSON.parse).then(console.log.bind(console));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment