1
votes

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 ?

3
When calling readValuesFromDB() you are not using an await, so it wont wait for the function to be finished there. PS: I would not mix async/await and Promise.then() syntax.Sirko
@Sirko that means the function in which readValuesFromDB() is called should be async too ?WLKB
Either that or use readValuesFromDB().then( () => console.log('finished readValuesFromDB()') ).Sirko
@Sirko. You're right with the mixin await/then. It's working but too much emphatic. I've edited my answer accordinglyBertrand
Don't use for…in enumerations on arrays! Also, why are you still using then when there is await?Bergi

3 Answers

3
votes

The 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)
});
3
votes

You are missing an await here:

(async function() {
  console.log('calling readValuesFromDB()');
  await readValuesFromDB();
  console.log('finished readValuesFromDB()');
})();
-1
votes

You have to await the call to readValuesFromDB.

console.log('calling readValuesFromDB()');
await readValuesFromDB();
console.log('finished readValuesFromDB()');