Last active
October 25, 2016 04:26
-
-
Save up1/a026a5b773201af1f2c2 to your computer and use it in GitHub Desktop.
Demo :: Asynchronous
This file contains 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
var fs = require("fs"); | |
fs.readFile("mydata.json", function(err, content) { | |
if (err) { | |
console.error("Got an error", err); | |
} else { | |
console.log("Got result"); | |
} | |
}); | |
console.log("After call readFile") |
This file contains 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
var FS = require('fs'), | |
request = require('request'); | |
function getResults(pathToFile, callback) { | |
FS.readFile(pathToFile, 'utf8', function(err, data) { | |
if (err) return callback(err); | |
var response1, response2; | |
request.post('http://www.thomas-bayer.com/sqlrest/', function(err, response, body) { | |
if(err) return callback(err); | |
response1 = response; | |
next(); | |
}); | |
request.post('http://www.thomas-bayer.com/sqlrest/', function(err, response, body) { | |
if(err) return callback(err); | |
response2 = response; | |
next(); | |
}); | |
function next(){ | |
if(response1 && response2){ | |
callback(null, [response1, response2]); | |
} | |
} | |
}); | |
} | |
getResults('./www/mydata.json', function(err, content) { | |
if (err) { | |
console.error("Got an error", err); | |
} else { | |
console.log("Got result "); | |
} | |
}); |
This file contains 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
asyncCall(function(err, data1){ | |
if(err) return callback(err); | |
anotherAsyncCall(function(err2, data2){ | |
if(err2) return calllback(err2); | |
oneMoreAsyncCall(function(err3, data3){ | |
if(err3) return callback(err3); | |
// TODO | |
}); | |
}); | |
}); |
This file contains 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
asyncCall() | |
.then(function(data1){ | |
// do something... | |
return anotherAsyncCall(); | |
}) | |
.then(function(data2){ | |
// do something... | |
return oneMoreAsyncCall(); | |
}) | |
.then(function(data3){ | |
// the third and final async response | |
}) | |
.fail(function(err) { | |
// handle any error resulting from any of the above calls | |
}) | |
.done(); |
This file contains 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
var FS = require('fs'), | |
Q = require('q'); | |
Q.nfcall(FS.readFile, "mydata.json", "utf-8") | |
.then(function(data) { | |
console.log('File has been read:', data); | |
}) | |
.fail(function(err) { | |
console.error('Error received:', err); | |
}) | |
.done(); |
This file contains 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
var FS = require('fs'), | |
Q = require('q'), | |
request = require('request'); | |
function getResults(pathToFile) { | |
return Q.nfcall(FS.readFile, pathToFile, "utf-8") | |
.then(function(repo) { | |
var options = { headers: {'User-Agent': 'MyAgent'} }; | |
return [Q.nfcall(request, 'https://api.github.com/repos/'+repo+'/collaborators', options), | |
Q.nfcall(request, 'https://api.github.com/repos/'+repo+'/commits', options)]; | |
}) | |
.spread(function(collaboratorsRes, commitsRes) { | |
return [collaboratorsRes[1], commitsRes[1]]; | |
}) | |
.fail(function(err) { | |
console.error(err) | |
return err; | |
}); | |
} | |
// actual call | |
getResults('repos.txt').then(function(responses) { | |
console.log("responses=" + responses) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment