Async.Series.js

'use strict'; var request = require('request'); var async = require('async'); var url = 'http://localhost:8080/'; async.series([ function(callback) { request.get(url + 'getUserName?id=1234', function(err, res, body) { console.log('Name:', JSON.parse(body).value); callback(null); }); }, function(callback) { request.get(url + 'getUserStatus?id=1234', function(err, res, body) { console.log('Status:', JSON.parse(body).value); callback(null); }); }, function(callback) { request.get(url + 'getUserCountry?id=1234', function(err, res, body) { console.log('Country:', JSON.parse(body).value); callback(null); }); }, function(callback) { request.get(url + 'getUserAge?id=1234', function(err, res, body) { console.log('Age:', JSON.parse(body).value); callback(null); }); } ]);
This is already starting to look messy, and we haven’t even added any notable “business logic” to our code. Note how our code is intended another level with every method call, creating the so-called “boomerang pattern” that is typical for multi-level nested callback control flows. We can use async.series to achieve the same control flow with much cleaner code

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.