Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Async mapi calls with js
var request = require('superagent');
var async = require('async');
var _ = require('lodash');
console.time('Execution Time');
baseUrl = "http://dev.mapi.move.com/properties/v1/search/"
limit = 20
var params = {
client_id: "doorsteps",
rental: "true",
listing_type: "rental",
map_friendly: true,
loc: 10010,
sort: "completeness,photos,freshest",
baths_min: 1,
price_max: 20000,
limit: limit,
}
var parallelRequests = [];
_.times(10, function(n){
parallelRequests.push(
function(callback){
params.offset = (n+1)*limit;
request.get(baseUrl)
.set('Accept-Encoding', 'gzip, deflate')
.set('Accept', 'application/json')
.query(params)
.end(function(err, res){
if ( err ) {
return callback(err);
} else {
return callback(null, res.body);
}
});
}
);
});
async.parallel(parallelRequests, function(err, resultsArray){
var combinedListings = resultsArray.map(function(result){
return result.listings;
}).reduce(function(prev, current) {
return prev.concat(current);
}, []);
console.timeEnd('Execution Time');
console.log('Listing count: ', combinedListings.length);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.