npm-promise-some
v0.0.2
Published
Resolve an array of promises n at a time
Downloads
7
Readme
Introduction
It will generate an array of promises and uses Promise.all to resolve n of them at a time. It is so as to accommodate physical limitations such as a cap on the number of API calls per second.
Input an array and a callback that will use the array element to generate a promise. The second callback is to handle how the data is to be processed. The last input n is the number of promises to resolve each time. It will then return an array of the concatenated outputs.
Example
'use strict';
const axios = require('axios');
var p = require('npm-promise-some');
var array = [1, 3, 7, 11, 51];
var url = 'https://jsonplaceholder.typicode.com/posts/';
function getPost(i) {
return axios.get(url + i);
}
function process(response) {
var items = response.map(item => item.data);
return items;
}
p.some(array, getPost, process, 2).then(function(results) {
console.log(results);
});