Skip to content

Instantly share code, notes, and snippets.

View seanstrom's full-sized avatar
:octocat:

Sean Hagstrom seanstrom

:octocat:
View GitHub Profile
@seanstrom
seanstrom / example.js
Created January 20, 2015 06:09
Async JS/Node - Sequential Async.js Example - Part 2
// Part 2
var username = 'seanghagstrom'
findLikesForPostsByUsername(username, function(err, totalLikes) {
if(err) {
console.error(err)
} else {
console.log(username + ' total number of likes: ' + totalLikes)
}
@seanstrom
seanstrom / example.js
Created January 20, 2015 06:08
Async JS/Node - Sequential Async.js Example - Part 1
// Part 1
function findLikesForPostsByUsername(username, mainCallback) {
async.waterfall([
function(callback) {
findUserByUserName(username, callback)
},
function(user, callback) {
findPostsByUser(user, callback)
},
@seanstrom
seanstrom / example.js
Created January 20, 2015 05:53
Async JS/Node - Parallel Callbacks Example - Part 3
// Part 3
var username = 'seanghagstrom'
getTweetsPostsRepos(username, function(err, tweetsPostsRepos) {
if(err) {
console.error(err)
} else {
console.log(username + ' has this stuff ' + tweetsPostsRepos)
}
@seanstrom
seanstrom / example.js
Created January 20, 2015 05:52
Async JS/Node - Parallel Callbacks Example - Part 2
// Part 2
function getTweetsPostsRepos(username, callback) {
// counter
getTweets(username, function(err, results) {
if(err) return callback(err)
somethingFinished(null, results)
})
@seanstrom
seanstrom / example.js
Created January 20, 2015 05:50
Async JS/Node - Parallel Callbacks Example - Part 1
// Part 1
function getTweetsPostsRepos(username, callback) {
var count = 0
, results = []
function somethingFinished(err, result) {
count += 1
results.push(result)
@seanstrom
seanstrom / example.js
Last active August 29, 2015 14:13
Async JS/Node - Sequential Callbacks Example - Part 2
// Part 2
var username = 'seanghagstrom'
findLikesForPostsByUsername(username, function(err, totalLikes) {
if(err) {
console.error(err)
} else {
console.log(username + ' total number of likes: ' + totalLikes)
}
@seanstrom
seanstrom / example.js
Created January 20, 2015 05:28
Async JS/Node - Sequential Callbacks Example - Part 1
// Part 1
function findLikesForPostsByUsername(username, callback) {
findUserByUsername(username, function(err, user) {
if(err) return callback(err)
findPostsByUser(user, function(err, posts) {
if(err) return callback(err)
findLikesByPosts(posts, function(err, likes) {
@seanstrom
seanstrom / example.js
Last active August 29, 2015 14:13
Async JS/Node - Node CPS Try-Catch example - Part 2
// Part 2
getFile(path, function(err, message) {
if(err) {
console.error(err)
} else {
console.log(message)
}
})
@seanstrom
seanstrom / example.js
Last active August 29, 2015 14:13
Async JS/Node - Node CPS Try-Catch example - Part 1
// Part 1
var fs = require('fs')
, path = 'my_file.txt'
function getFile(path, callback) {
try {
fs.readFile(path, function(err, data) {
if(err) throw err
var wrapperMessage = {coolStuff: true, data: data}
@seanstrom
seanstrom / example.js
Created January 20, 2015 03:48
Async JS/Node - Node Callbacks - Don't use return values from Callbacks example
var fs = require('fs')
, path = 'my_file.txt'
, file
file = fs.readFile(path, function(err, fileData) {
if(err) {
console.log(err)
} else {
console.log('success')
}