admin管理员组文章数量:1123011
I have a Javascript function that takes in an int argument and returns a promise. I also have an array of ints(variable length) and I would like to call my function multiple times passing in each int to it and return multiple promises in one function. How do I accomplish this? The closest I could find on SO was JS return multiple promises in one function
function myFunction(input)
{
return Promise.resolve(input);
}
/*
function invokedFunction(input)
{
return function myFunction() {return Promise.resolve(input);}
}
function waitForAll()
{
var resultArray = [];
resultArray = await callMyFunction();
}
*/
function callMyFunction(inputArray)
{
Promise.all[myFunction(pass in each item from inputArray)..... ];
/*
const promisesArray = [];
if(Array.isArray(inputArray)){
inputArray.forEach(function(item, index){
promisesArray[index] = invokedFunction(item);
});
}
return Promise.all(promisesArray);
*/
}
var inputArray = [1, 2, 3, 4, 5];
callMyFunction(inputArray);
I have a Javascript function that takes in an int argument and returns a promise. I also have an array of ints(variable length) and I would like to call my function multiple times passing in each int to it and return multiple promises in one function. How do I accomplish this? The closest I could find on SO was JS return multiple promises in one function
function myFunction(input)
{
return Promise.resolve(input);
}
/*
function invokedFunction(input)
{
return function myFunction() {return Promise.resolve(input);}
}
function waitForAll()
{
var resultArray = [];
resultArray = await callMyFunction();
}
*/
function callMyFunction(inputArray)
{
Promise.all[myFunction(pass in each item from inputArray)..... ];
/*
const promisesArray = [];
if(Array.isArray(inputArray)){
inputArray.forEach(function(item, index){
promisesArray[index] = invokedFunction(item);
});
}
return Promise.all(promisesArray);
*/
}
var inputArray = [1, 2, 3, 4, 5];
callMyFunction(inputArray);
Share
Improve this question
asked 36 mins ago
SoftwareDveloperSoftwareDveloper
7203 gold badges8 silver badges30 bronze badges
2
|
1 Answer
Reset to default 0var inputArray = [1, 2, 3, 4, 5];
function getPromiseArray() {
// Can use a for...of instead of .map if preferred
const promises = inputArray.map((item) => callMyFunction(item));
return promises;
}
Then if you want to await all promises
let results = await Promise.all(getPromiseArray())
Promise.all returns an array of the fulfillment values
A example function to just loop through the results
async function processPromises() {
try {
// Wait for all promises to resolve
const results = await Promise.all(getPromiseArray());
// Log each result
for (let result of results) {
console.log(result);
}
} catch (error) {
console.error("An error occurred while processing promises:", error);
}
}
Note:
- When using promise.all, it’s worth noting that if any promise in the input array rejects, the whole Promise.all will reject.
本文标签: Javascript passing in a dynamic array returning multiple promises in a functionStack Overflow
版权声明:本文标题:Javascript: passing in a dynamic array: returning multiple promises in a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736539603a1944369.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
return Promise.all(inputArray.map(item => myFunction(item)))
– Guy Incognito Commented 13 mins agoarray.map()
to call a function that returns a promise for each array element. – Barmar Commented 13 mins ago