Skip to content

Instantly share code, notes, and snippets.

@getify
Forked from tomasdev/nasty.js
Last active August 29, 2015 14:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getify/88faca21831f3bde27d5 to your computer and use it in GitHub Desktop.
Save getify/88faca21831f3bde27d5 to your computer and use it in GitHub Desktop.
function segmentArray(arr,segmentSize) {
segmentSize = Math.max(1,Math.min(+segmentSize||0,arr.length));
return Array.apply(null,{length:Math.ceil(arr.length / segmentSize)}).map(function(_,i){
return arr.slice(i*segmentSize,(i+1)*segmentSize);
});
}
// lift `serviceCall(..)`
function promiseServiceCall(v) {
return new Promise(function(resolve,reject){
serviceCall(v,function(err,r){
if (err) reject(err);
else resolve(r);
});
});
}
var fetchAll = function (allUrls) {
// `all(..)` waits for all to succeed
// note: use `any(..)` for allowing some failures
return Promise.all(
// break `allUrls` into batches
segmentArray( allUrls, 5 )
// turn each batch of URLs into a request
.map(function mapper(arr){
return promiseServiceCall({
Action: 'UrlInfo',
'UrlInfo.Shared.ResponseGroup': 'Rank',
'UrlInfo.1.Url': arr[0],
'UrlInfo.2.Url': arr[1],
'UrlInfo.3.Url': arr[2],
'UrlInfo.4.Url': arr[3],
'UrlInfo.5.Url': arr[4]
})
// OPTIONAL: observation step
.then(
// observe success
function addData(data){
console.log("..");
// propagate success
return data;
},
// observe error
function error(err){
console.error(err);
// propagate error
throw err;
}
)
})
)
// combine all the batch results
.then(function combine(results){
return results.reduce(function reducer(acc,curr){
return acc.concat(curr);
},[]);
});
};
@tomasdev
Copy link

The segmentArray is quite neat. And I love your map-reduce version. Seriously appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment