Last active
December 24, 2015 09:59
-
-
Save radiosilence/6780768 to your computer and use it in GitHub Desktop.
Three ways of chaining callbacks that are dependant on the previous one. In CoffeeScript.
This file contains hidden or 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
http = require 'http' | |
yellIP = (callbacks, data) -> | |
console.log "OI!! YOUR IP IS #{data.ip}!!!" | |
printIP = (callbacks, data) -> | |
console.log "Your IP address: #{data.ip}" | |
parseJSON = (callbacks, string) -> | |
callbacks.shift()(callbacks, JSON.parse(string)) | |
getIP = (callbacks) -> | |
http.get | |
host: 'ip.jsontest.com' | |
path: '/' | |
, (res) -> | |
str = '' | |
res.on 'data', (chunk) -> | |
str += chunk | |
res.on 'end', -> | |
callbacks.shift()(callbacks, str) | |
getIP [ | |
parseJSON | |
printIP | |
] | |
getIP [ | |
parseJSON | |
yellIP | |
] |
This file contains hidden or 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
http = require 'http' | |
async = require 'async' | |
yellIP = (data) -> | |
console.log "OI!! YOUR IP IS #{data.ip}!!!" | |
printIP = (data) -> | |
console.log "Your IP address: #{data.ip}" | |
parseJSON = (string, callback) -> | |
callback null, JSON.parse(string) | |
getIP = (callback) -> | |
http.get | |
host: 'ip.jsontest.com' | |
path: '/' | |
, (res) -> | |
str = '' | |
res.on 'data', (chunk) -> | |
str += chunk | |
res.on 'end', -> | |
callback null, str | |
async.waterfall [ | |
getIP | |
parseJSON | |
printIP | |
] | |
async.waterfall [ | |
getIP | |
parseJSON | |
yellIP | |
] |
This file contains hidden or 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
http = require 'http' | |
Q = require 'q' | |
yellIP = (data) -> | |
deferred = Q.defer() | |
console.log "OI!! YOUR IP IS #{data.ip}!!!" | |
deferred.promise | |
printIP = (data) -> | |
deferred = Q.defer() | |
console.log "Your IP address: #{data.ip}" | |
deferred.promise | |
parseJSON = (string) -> | |
deferred = Q.defer() | |
deferred.resolve JSON.parse(string) | |
deferred.promise | |
getIP = -> | |
deferred = Q.defer() | |
http.get | |
host: 'ip.jsontest.com' | |
path: '/' | |
, (res) -> | |
str = '' | |
res.on 'data', (chunk) -> | |
str += chunk | |
res.on 'end', -> | |
deferred.resolve str | |
deferred.promise | |
getIP() | |
.then(parseJSON) | |
.then(printIP) | |
getIP() | |
.then(parseJSON) | |
.then(yellIP) |
This file contains hidden or 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
from twisted.internet import reactor | |
from twisted.web import client | |
from twisted.internet import defer | |
import json | |
def parseJSON(page): | |
return json.loads(page) | |
def printIP(data): | |
print ' '.join(['Your IP address:', data.get('ip')]) | |
def yellIP(data): | |
print ' '.join(['OI!! YOUR IP IS', data.get('ip'), '!!!']) | |
def getIP(): | |
d = defer.Deferred() | |
url = 'http://ip.jsontest.com/' | |
client.getPage(url) \ | |
.addCallback(d.callback) | |
return d | |
def stopReactor(_): | |
reactor.stop() | |
if __name__ == '__main__': | |
defer.DeferredList([ | |
getIP() \ | |
.addCallback(parseJSON) \ | |
.addCallback(printIP), | |
getIP() \ | |
.addCallback(parseJSON) \ | |
.addCallback(yellIP), | |
]).addCallback(stopReactor) | |
reactor.run() |
This file contains hidden or 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
pg = require 'pg' | |
async = require 'async' | |
conString = "postgres://qrs:qrs@localhost/qrs" | |
getClient = (callback) -> | |
pg.connect conString, (err, client, done) -> | |
callback(err, client, done) | |
doQuery = (client, done, callback) -> | |
client.query 'SELECT * FROM instruments_instrument LIMIT 5', (err, result) -> | |
done() | |
callback err, result | |
printRes = (result, callback) -> | |
console.log "ROWS:", result.rows | |
callback null, result | |
async.waterfall [ | |
getClient | |
doQuery | |
printRes | |
], (err, result) -> | |
if err | |
console.log "ERROR:", err |
This file contains hidden or 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
http = require 'http' | |
util = require 'util' | |
net = require 'net' | |
{exec} = require 'child_process' | |
pg = require('pg').native | |
async = require 'async' | |
conString = "postgres://qrs:qrs@localhost/qrs" | |
reportError = (res, err) -> | |
res.writeHead 500, | |
'content-type': 'application/json' | |
res.end JSON.stringify err | |
respondJSON = (res, data) -> | |
res.writeHead 200, | |
'content-type': 'application/json' | |
res.end JSON.stringify data | |
getClient = (callback) -> | |
pg.connect conString, (err, client, done) -> | |
callback(err, client, done) | |
doQuery = (client, done, callback) -> | |
client.query 'SELECT id FROM instruments_instrument LIMIT 1', (err, result) -> | |
done() | |
callback err, result.rows | |
httpServer = http.Server (req, res) -> | |
respond = (err, data) -> | |
if err | |
reportError(res, err) | |
else | |
respondJSON(res, data) | |
async.waterfall [ | |
getClient | |
doQuery | |
], respond | |
httpServer.listen 1337 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment