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 03:36
Async JS/Node - Node CPS More Error Handling example - Part 2
// Part 2
getFile(path, function(err, message) {
if(err) {
console.error(err)
} else {
console.log(message)
}
})
@seanstrom
seanstrom / example.js
Created January 20, 2015 03:33
Async JS/Node - Node CPS More Error Handling example - Part 1
// 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)
}
@seanstrom
seanstrom / example.js
Last active August 29, 2015 14:13
Async JS/Node - Parallel Promises Example
function getTweetsPostsRepos(username) {
var tweets = getTweets(username)
var posts = getPosts(username)
var repos = getRepos(username)
return Promise.all([tweets, posts, repos])
}
var username = 'seanghagstrom'
@seanstrom
seanstrom / example.js
Last active August 29, 2015 14:13
Async JS/Node - Sequential Promises Example
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) {
@seanstrom
seanstrom / example.js
Last active August 29, 2015 14:13
Async JS/Node - Promises Always Async example
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)
@seanstrom
seanstrom / example.js
Last active August 29, 2015 14:13
Async JS/Node - Callbacks Always Async example
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) {
@seanstrom
seanstrom / example.js
Last active August 29, 2015 14:13
Async JS/Node - Callbacks Not Always Async example
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)
@seanstrom
seanstrom / example.js
Last active August 29, 2015 14:13
Async JS/Node - Promise Thrown Error Handling example
function throwsError() {
throw new Error('silly error')
}
var endpoint = 'my/endpoint'
var dataPromise = getData(endpoint)
dataPromise
.then(function(data) {
return throwsError()
@seanstrom
seanstrom / example.js
Last active August 29, 2015 14:13
Async JS/Node - Promise Basic Example
var fs = require('fs-promise')
, path = 'my_file.txt'
var filePromise = fs.readFile(path)
filePromise.then(
function(fileData) {
console.log('done')
},
function(error) {
@seanstrom
seanstrom / example.js
Last active August 29, 2015 14:13
Async JS/Node - Parallel Async.js Example
function getTweetsPostsRepos(username, mainCallback) {
async.parallel([
function(callback) {
getTweets(username, callback)
},
function(callback) {
getPosts(username, callback)
},
function(callback) {
getRepos(username, callback)