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 / composer.js
Last active August 29, 2015 14:12
Exploring Constructors and Factories in Javascript - Factories With Object.create Example
var extend = require('./extend')
function composer(obj) {
obj = obj || {}
return {
extend: function(extension) {
var extended = extend(obj, extension)
return composer(extended)
},
@seanstrom
seanstrom / example.rb
Last active August 29, 2015 14:13
Async JS/Node - Sync vs Async example
def greeter_sync
print(“Hello World”)
sleep(5000) # sleep for 5 seconds
print(“Goodbye World”)
end
def greeter_async
# sleep for 5 seconds, then print Goodbye World
async sleep(5000) then print(“Goodbye World”)
print(“Hello World”)
@seanstrom
seanstrom / Output
Created January 13, 2015 21:43
Async JS/Node - Sync vs Async Print example
Hello World
# ... 5 seconds or so
Goodbye World
@seanstrom
seanstrom / example.js
Last active August 29, 2015 14:13
Async JS/Node - readFile typo example
var fs = require('fs')
, path = 'my_file.txt'
, file
file = fs.readFile(path)
@seanstrom
seanstrom / example.js
Created January 14, 2015 03:36
Async JS/Node - readFile fixed example
var fs = require('fs')
, path = 'my_file.txt'
fs.readFile(path, function() {
console.log('done')
})
@seanstrom
seanstrom / example.js
Last active August 29, 2015 14:13
Async JS/Node - readFile Node CPS example
var fs = require('fs')
, path = 'my_file.txt'
fs.readFile(path, function(err, data) {
if(err) {
console.error(err)
} else {
console.log(data)
}
})
@seanstrom
seanstrom / example.js
Last active August 29, 2015 14:13
Async JS/Node - Node CPS More Error Handling example
var fs = require('fs')
, path = 'my_file.txt'
function getFile(path, callback) {
fs.readFile(path, function(err, data) {
if(err){
return callback(err)
}
var wrapperMessage = {coolStuff: true, data: data}
@seanstrom
seanstrom / example.js
Created January 14, 2015 05:43
Async JS/Node - Node CPS Try-Catch example
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}
callback(null, wrapperMessage)
})
@seanstrom
seanstrom / example.js
Last active August 29, 2015 14:13
Async JS/Node - Sequential Callbacks Example
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) {
if(err) return callback(err)
@seanstrom
seanstrom / example.js
Last active August 29, 2015 14:13
Async JS/Node - Parallel Callbacks Example
function getTweetsPostsRepos(username, callback) {
var count = 0
, results = []
function somethingFinished(err, result) {
count += 1
results.push(result)
if(count === 3) {
callback(null, results)