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
// Part 2 | |
getFile(path, function(err, message) { | |
if(err) { | |
console.error(err) | |
} else { | |
console.log(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
// Part 1 | |
var fs = require('fs') | |
, path = 'my_file.txt' | |
function getFile(path, callback) { | |
fs.readFile(path, function(err, data) { | |
if(err){ | |
return callback(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
function getTweetsPostsRepos(username) { | |
var tweets = getTweets(username) | |
var posts = getPosts(username) | |
var repos = getRepos(username) | |
return Promise.all([tweets, posts, repos]) | |
} | |
var username = 'seanghagstrom' |
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
function findLikesForPostsByUsername(username, callback) { | |
var promise = new Promise(function(resolver, rejector) { | |
findUserByUsername(username) | |
.then(findPostsByUser) | |
.then(findLikesByPosts) | |
.then(function(likes) { | |
var totalLikes = likes.reduce(function(x, y) { return x + y }) | |
resolver(totalLikes) | |
}) | |
.catch(function(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 http = require('special-http') | |
function getData(endpoint, callback) { | |
var promise = new Promise(function(resolver, rejector) { | |
if(cache[endpoint]) { | |
return resolver(cache[endpoint]) | |
} | |
http.get(endpoint).then(function(data) { | |
resolver(data) |
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 http = require('special-http') | |
function getData(endpoint, callback) { | |
if(cache[endpoint]) { | |
return setTimeout(function() { | |
callback(null, cache[endpoint]) | |
}, 0) | |
} | |
http.get(endpoint, function(err, data) { |
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 http = require('special-http') | |
function getData(endpoint, callback) { | |
if(cache[endpoint]) { | |
return callback(null, cache[endpoint]) | |
} | |
http.get(endpoint, function(err, data) { | |
if(err) { | |
return callback(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
function throwsError() { | |
throw new Error('silly error') | |
} | |
var endpoint = 'my/endpoint' | |
var dataPromise = getData(endpoint) | |
dataPromise | |
.then(function(data) { | |
return throwsError() |
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 fs = require('fs-promise') | |
, path = 'my_file.txt' | |
var filePromise = fs.readFile(path) | |
filePromise.then( | |
function(fileData) { | |
console.log('done') | |
}, | |
function(error) { |
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
function getTweetsPostsRepos(username, mainCallback) { | |
async.parallel([ | |
function(callback) { | |
getTweets(username, callback) | |
}, | |
function(callback) { | |
getPosts(username, callback) | |
}, | |
function(callback) { | |
getRepos(username, callback) |