Created
May 19, 2015 17:12
-
-
Save kjs3/a3c17b6e97b1f94948ee to your computer and use it in GitHub Desktop.
Async mapi calls with js
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 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