Created
March 28, 2019 17:42
-
-
Save SarasArya/586f5886d1d0f0b647df110b3bef8bcb to your computer and use it in GitHub Desktop.
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
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