Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Execute promises sequentially (one at at a time) and return array with the results
function promiseMap(inputValues, mapper) {
const reducer = (acc$, inputValue) =>
acc$.then(acc => mapper(inputValue).then(result => acc.push(result) && acc));
return inputValues.reduce(reducer, Promise.resolve([]));
/* Example */
const axios = require('axios');
function countryFromIp(ip) {
return axios.get(`${ip}`)
.then(res =>;
promiseMap(["", "", ""], countryFromIp).then(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment