I have created a function to loop through an array, call a promise, and update a variable based on the result. The code seems to be functioning correctly, but I am wondering if there is a more optimal way to write it. Any suggestions are appreciated. Thank you!
/**
*
* u/returns Resolves a promise and returns value 'b'
*/
async function generatePromise() {
return Promise.resolve('b');
}
async function main() {
let showWarning = false;
// List of users
const users = ['a', 'b', 'c'];
//Looping through users array and compare the result from promise and update showWarning boolean
await users.reduce(async (promise, user) => {
await promise;
const result = await generatePromise();
if (result === user) showWarning = true;
}, Promise.resolve())
console.log(showWarning)
}
main();