function giveMeName() {
var results = [];
return arr.reduce(function(res, item) {
return res ? res.then(function(res) {
results.push(res);
return item();
}) : item();
}, null).then(function(res) {
results.push(res);
return results;
});
}
const arr = [() => Promise.resolve(5), () => 6, () => 24]
const arr = [() => Promise.resolve(5), () => 6, () => 24]
giveMeName() // [5, 6, 24]
const arr = [
() => new Promise(
r => setTimeout(() => r("Petrov"), 1000)
),
() => new Promise(
r => setTimeout(() => r("Petr"), 1000)
),
() => new Promise(
r => setTimeout(() => r("Petrovich"), 1000)
)
];
giveMeName().then(r => console.log(r.join(" "))) // Petrov Petr Petrovich
const fetch1 = () => fetch(url1).then(r => r.json());
const fetch2 = () => fetch(url2).then(r => r.json());
const arr = [fetch1, fetch2];