let locArr = [{ x: 0, y: 0 }, { x: 2, y: 4 }, { x: 6, y: 8 }];
// my asynchronous function that returns a promise
function findLoc(x, y) {
return new Promise((resolve, reject) => {
let a = setTimeout(() => {
resolve({ x: x * x, y: y * y });
}, 500);
});
}
Promise.all([
// my problem is below
findLoc(locArr[0].x, locArr[0].y),
findLoc(locArr[1].x, locArr[1].y),
findLoc(locArr[2].x, locArr[2].y),
]).then(values => {
// all values from all the promises
});
How can I write the Promise.all()
function to take parameters from an array of varying size?
I want to pass arguments to the array of promises accepted in the .all()
method of the Promise
class. What is the best way to do that?
let locArr = [{ x: 0, y: 0 }, { x: 2, y: 4 }, { x: 6, y: 8 }];
// my asynchronous function that returns a promise
function findLoc(x, y) {
return new Promise((resolve, reject) => {
let a = setTimeout(() => {
resolve({ x: x * x, y: y * y });
}, 500);
});
}
Promise.all([
// my problem is below
findLoc(locArr[0].x, locArr[0].y),
findLoc(locArr[1].x, locArr[1].y),
findLoc(locArr[2].x, locArr[2].y),
]).then(values => {
// all values from all the promises
});
How can I write the Promise.all()
function to take parameters from an array of varying size?
I want to pass arguments to the array of promises accepted in the .all()
method of the Promise
class. What is the best way to do that?
- 1 This code looks OK at a glance, what's the issue? – Andy Ray Commented Apr 10, 2018 at 3:15
-
2
you should create array
const x = locArr.map(a => findLoc(a.x, a.y))
and passx
inside Promise.all – indapublic Commented Apr 10, 2018 at 3:16 - @andy I don't want to hardcode the array values that are passed into the promises – Emmanuel Commented Apr 10, 2018 at 3:16
- @indapublic could you explain that a bit more? – Emmanuel Commented Apr 10, 2018 at 3:19
- 1 @EmmanuelNK Try this Promise.all(locArr.map(a => findLoc(a.x, a.y))).then(values => { console.log(values); }); – indapublic Commented Apr 10, 2018 at 3:21
4 Answers
Reset to default 10use map
instead
let locArr = [{
x: 0,
y: 0
}, {
x: 2,
y: 4
}, {
x: 6,
y: 8
}];
// my asynchronous function that returns a promise
function findLoc(x, y) {
return new Promise((resolve, reject) => {
let a = setTimeout(() => {
resolve({
x: x * x,
y: y * y
});
}, 500);
});
}
Promise.all(
locArr.map(o => findLoc(o.x, o.y))
).then(values => {
// all values from all the promises
console.log(values)
});
You can use map:
let locArr = [{ x: 0, y: 0 }, { x: 2, y: 4 }, { x: 6, y: 8 }];
// my asynchronous function that returns a promise
function findLoc(x, y) {
return new Promise((resolve, reject) => {
let a = setTimeout(() => {
resolve({ x: x * x, y: y * y });
}, 500);
});
}
Promise.all(
locArr.map(//map location to promise
location=>findLoc(location.x,location.y)
)
).then(values => {
// all values from all the promises
});
This is a trick that requires to modify a little the input of findLoc
:
let locArr = [{ x: 0, y: 0 }, { x: 2, y: 4 }, { x: 6, y: 8 }];
// my asynchronous function that returns a promise
//
function findLoc({x, y}) {
return new Promise((resolve, reject) => {
let a = setTimeout(() => {
resolve({ x: x * x, y: y * y });
}, 500);
});
}
Promise.all(locArr.map(findLoc)).then(values => {
console.log(values);
});
Use map, like others said, but also adjust the findLoc()
function for the simplest reading:
let locArr = [{ x: 0, y: 0 }, { x: 2, y: 4 }, { x: 6, y: 8 }];
// my asynchronous function that returns a promise
function findLoc(coordinates) {
return new Promise((resolve, reject) => {
let a = setTimeout(() => {
resolve({
x: coordinates.x * coordinates.x,
y: coordinates.y * coordinates.y
});
}, 500);
});
}
Promise
.all(locArr.map(findLoc))
.then(values => {
console.log(JSON.stringify(values, false, 2));
});