Skip to content

Instantly share code, notes, and snippets.

@SarasArya
Created March 28, 2019 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SarasArya/586f5886d1d0f0b647df110b3bef8bcb to your computer and use it in GitHub Desktop.
Save SarasArya/586f5886d1d0f0b647df110b3bef8bcb to your computer and use it in GitHub Desktop.
const rp = require('request-promise');
const urls = ['https://jsonplaceholder.typicode.com/todos/1', 'https://jsonplaceholder.typicode.com/todos/2'];
const newUrls = urls.map(url => () => rp(url));
Promise.resolve()
.then(x => newUrls[0]())
.then(x => newUrls[1]());
console.log(newUrls);
Promise.resolve([])
.then(all => {
return newUrls[0]().then(result => all.concat(result));
})
.then(all => {
return newUrls[1]().then(result => all.concat(result));
});
// const rrr = [];
// Promise.resolve(rrr)
// .then(x => newUrls[0]().then(Array.prototype.concat.bind(x)))
// .then(x => newUrls[1]().then(Array.prototype.concat.bind(x)))
// .then(result => console.log(result))
const promiseSerial = newUrls =>
newUrls.reduce((acc, newUrl) => {
console.log(acc);
return Promise.resolve(acc).then(x => newUrl().then(Array.prototype.concat.bind(x)))
}, Promise.resolve([]));
promiseSerial(newUrls).then(result => console.log(result));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment