Last active
December 10, 2016 09:42
-
-
Save nervetattoo/97692f4823c84afad3bf to your computer and use it in GitHub Desktop.
promises-promises-promises
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
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':'[email protected]'}); | |
}] | |
}, function(err, results) { | |
console.log('err = ', err); | |
console.log('results = ', results); | |
}); |
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
fs.readFile('data.json', function(err, data) { | |
if (err) // handle it | |
try { | |
var json = JSON.parse(data); | |
render(json); | |
} | |
catch (e) { // handle it }} | |
}); |
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(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); | |
} | |
} | |
}); | |
} | |
}); | |
} | |
}); |
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 squareCb = function(num, cb) { | |
cb(null, num * num); | |
}; | |
squareCb(2, function(err, num) { | |
squareCb(num, function(err, result) { | |
logIt(result); | |
}); | |
}); |
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 square = function(num) { | |
return Promise.resolve(num * num); | |
} | |
square(2).then(square).then(logIt); |
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 nestedPromises = function() { | |
return new Promise(function(resolve, reject) { | |
new Promise(function(resolve2, reject2) { | |
resolve2("Hello world"); | |
}).then(resolve, reject) | |
}) | |
} | |
nestedPromises().then(logIt); |
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(userId) | |
.then(function(user) { | |
return getUserFriends(user); | |
}) | |
.then(function(friends) { | |
return getUserProfile(friends[0]); | |
}) | |
.then(JSON.parse) | |
.then(frefreshUi, handleError); |
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 square = function(num) { | |
return num * num; | |
} | |
Promise.resolve(square(2)).then(square).then(logIt); |
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
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); }); |
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'); | |
// 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