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
Last active August 29, 2015 14:13
Async JS/Node - Using Promise Example
var endpoint = 'my/endpoint'
var dataPromise = getData(endpoint)
dataPromise.then(function(data) {
console.log(data)
}, function(error) {
console.error(error)
})
@seanstrom
seanstrom / example.js
Created January 20, 2015 16:19
Async JS/Node - Create Promise Example
var http = require('special-http')
function getData(endpoint) {
var promise = new Promise(function(resolver, rejector) {
http.get(endpoint).then(function(data) {
resolver(data)
}, function(err) {
rejector(err)
})
})
@seanstrom
seanstrom / example.js
Last active August 29, 2015 14:13
Async JS/Node - Parallel Async.js Example - Part 2
// Part 2
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 06:26
Async JS/Node - Parallel Async.js Example - Part 1
// Part 1
function getTweetsPostsRepos(username, mainCallback) {
async.parallel([
function(callback) {
getTweets(username, callback)
},
function(callback) {
getPosts(username, callback)
},
@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)
}