Last active
September 19, 2017 00:38
-
-
Save mchelen/16463df393bf3be7aa896f6909644400 to your computer and use it in GitHub Desktop.
Node async parallel example
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
node_modules |
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
var request = require('request'); | |
var async = require('async'); | |
async.parallel( | |
[ | |
function(callback) { | |
request('https://jsonplaceholder.typicode.com/posts/1', function (error, response, body) { | |
var data = JSON.parse(body); | |
callback(null, data); | |
}); | |
}, | |
function(callback) { | |
request('https://jsonplaceholder.typicode.com/posts/2', function (error, response, body) { | |
var data = JSON.parse(body); | |
callback(null, data); | |
}); | |
} | |
], | |
function(err, results) { | |
results.forEach(function(element) { | |
console.log("title: ", element.title); | |
}); | |
} | |
); | |
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
{ | |
"name": "node-async", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"async": "^2.5.0", | |
"request": "^2.81.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment