Reputation: 187
I am mapping though an array and then calling three async calls that return the parameter information to make another call. I want the code to take one account at a time and go through each async call before continuing to the next account in the array. The problem is right now the code is running all accounts through the first async call then moves to the second async call runs all the accounts through and the same for the third. Here is what I have tried so far:
const request = require('request')
const delay = (ms) => {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
async function report(){
let accounts = await getAccountsForTimezone('Eastern Time (US & Canada)');
const promises = accounts.map((account) => {
return new Promise(async (resolve, reject) => {
await delay(1000)
const param1 = await scope1(account)
const param2 = await scope2(account)
const param3 = await scope3(account)
const response = insertData(param1, param2, param3).catch(reject)
resolve(response)
})
})
};
I also tried:
async function report(){
let accounts = await getAccountsForTimezone('Eastern Time (US & Canada)');
const promises = accounts.map((account) => {
return new Promise(async (resolve, reject) => {
await delay(1000)
const reults = Promise.all([await scope1(account), await scope2.apply(account), scope3.apply(account)])
const response = insertData(param1, param2, param3).catch(reject)
resolve(response)
})
})
};
I expect the loop to look like: Account 1 ->param1 ->param2 ->param3
Account 2 ->param1 ->param2 ->param3
Instead I get: Account 1 ->param1 Account 2 ->param1
Account 1 ->param2 Account 2 ->param2
Account 1 ->param3 Account 2 ->param3
Upvotes: 0
Views: 254
Reputation: 1
Since you're not using the promises
return in accounts.map
there's no reason to use map at all
a simple for...of
loop is probably what you want
async function report(){
let accounts = await getAccountsForTimezone('Eastern Time (US & Canada)');
for (const account of accounts) {
await delay(1000)
const param1 = await scope1(account)
const param2 = await scope2(account)
const param3 = await scope3(account)
const response = insertData(param1, param2, param3).catch(reject)
}
};
Upvotes: 1