Below is my async function to read values from DB and logs something on console. But for some reason its not happening.
So, I create an array of promises and then keep waiting for all promises to resolve as promises will read from DB. But await on Promise.all is not pausing the execution of code and not waiting for all promises to resolve instead its passing control back to calling function. Is there anything I am missing here ?
async function readValuesFromDB(codes) {
console.log('trying to read values from DB');
let dbPromises = [];
for(let code in codes) {
let refId = 'some_random_reference_id_generated_from_codes[i]';
dbPromises.push(MyDb.getPromise(refId)); // getPromise() returns a promise that reads from DB
}
console.log('waiting for all promises to resolve...');
// waiting for all promises to finish
await Promise.all(dbPromises).then((allPromises) => {
for(let i in allPromises) {
console.log('OK..read from DB');
}
});
console.log('all promises resolved');
}
console.log('calling readValuesFromDB()');
readValuesFromDB();
console.log('finished readValuesFromDB()');
Output from above call is:
trying to read values from DB
waiting for all promises to resolve...
finished readValuesFromDB
......
......
OK..read from DB
OK..read from DB
OK..read from DB
...
...
all promises resolved
Ideally output should be below (since I am waiting for all promises to resolve using await with Promise.all)
trying to read values from DB
waiting for all promises to resolve...
OK..read from DB
OK..read from DB
OK..read from DB
...
...
So looks like promises are getting resolved after the control is passed back to calling function and seems like await has no effect here.
Any help on why await is not in effect in this case ?
Below is my async function to read values from DB and logs something on console. But for some reason its not happening.
So, I create an array of promises and then keep waiting for all promises to resolve as promises will read from DB. But await on Promise.all is not pausing the execution of code and not waiting for all promises to resolve instead its passing control back to calling function. Is there anything I am missing here ?
async function readValuesFromDB(codes) {
console.log('trying to read values from DB');
let dbPromises = [];
for(let code in codes) {
let refId = 'some_random_reference_id_generated_from_codes[i]';
dbPromises.push(MyDb.getPromise(refId)); // getPromise() returns a promise that reads from DB
}
console.log('waiting for all promises to resolve...');
// waiting for all promises to finish
await Promise.all(dbPromises).then((allPromises) => {
for(let i in allPromises) {
console.log('OK..read from DB');
}
});
console.log('all promises resolved');
}
console.log('calling readValuesFromDB()');
readValuesFromDB();
console.log('finished readValuesFromDB()');
Output from above call is:
trying to read values from DB
waiting for all promises to resolve...
finished readValuesFromDB
......
......
OK..read from DB
OK..read from DB
OK..read from DB
...
...
all promises resolved
Ideally output should be below (since I am waiting for all promises to resolve using await with Promise.all)
trying to read values from DB
waiting for all promises to resolve...
OK..read from DB
OK..read from DB
OK..read from DB
...
...
So looks like promises are getting resolved after the control is passed back to calling function and seems like await has no effect here.
Any help on why await is not in effect in this case ?
Share Improve this question asked May 17, 2018 at 12:00 WLKBWLKB 111 silver badge3 bronze badges 6-
1
When calling
readValuesFromDB()
you are not using anawait
, so it wont wait for the function to be finished there. PS: I would not mixasync/await
andPromise.then()
syntax. – Sirko Commented May 17, 2018 at 12:05 -
@Sirko that means the function in which
readValuesFromDB()
is called should beasync too
? – WLKB Commented May 17, 2018 at 12:11 -
1
Either that or use
readValuesFromDB().then( () => console.log('finished readValuesFromDB()') )
. – Sirko Commented May 17, 2018 at 12:13 - @Sirko. You're right with the mixin await/then. It's working but too much emphatic. I've edited my answer accordingly – Bertrand Commented May 17, 2018 at 12:35
-
Don't use
for…in
enumerations on arrays! Also, why are you still usingthen
when there isawait
? – Bergi Commented May 17, 2018 at 12:46
3 Answers
Reset to default 3The behaviour is normal.
readValuesFromDB
is ran async, not console.log('finished readValuesFromDB()');
So console is parsing "finished" before readValuesFrom DB resolves.
You can do like this :
async function readValuesFromDB(codes) {
console.log('trying to read values from DB');
let dbPromises = [];
for(let code in codes) {
let refId = 'some_random_reference_id_generated_from_codes[i]';
dbPromises.push(MyDb.getPromise(refId)); // getPromise() returns a promise that reads from DB
}
console.log('waiting for all promises to resolve...');
// waiting for all promises to finish
let allPromises = await Promise.all(dbPromises);
for(let i in allPromises) {
console.log('OK..read from DB');
}
console.log('all promises resolved');
console.log('finished readValuesFromDB()');
}
console.log('calling readValuesFromDB()');
readValuesFromDB();
or like this :
async function readValuesFromDB(codes) {
console.log('trying to read values from DB');
let dbPromises = [];
for(let code in codes) {
let refId = 'some_random_reference_id_generated_from_codes[i]';
dbPromises.push(MyDb.getPromise(refId)); // getPromise() returns a promise that reads from DB
}
console.log('waiting for all promises to resolve...');
// waiting for all promises to finish
let allPromises = await Promise.all(dbPromises);
for(let i in allPromises) {
console.log('OK..read from DB');
}
console.log('all promises resolved');
allPromises = ['a', 'b', 'c']; // Dumb values for example
return allPromises;
}
console.log('calling readValuesFromDB()');
readValuesFromDB().then(response => {
console.log('finished readValuesFromDB()');
console.log(response)
});
You are missing an await here:
(async function() {
console.log('calling readValuesFromDB()');
await readValuesFromDB();
console.log('finished readValuesFromDB()');
})();
You have to await
the call to readValuesFromDB
.
console.log('calling readValuesFromDB()');
await readValuesFromDB();
console.log('finished readValuesFromDB()');