最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - await with Promise.all is not in effect - Stack Overflow

programmeradmin0浏览0评论

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 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 Commented May 17, 2018 at 12:05
  • @Sirko that means the function in which readValuesFromDB() is called should be async 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 using then when there is await? – Bergi Commented May 17, 2018 at 12:46
 |  Show 1 more ment

3 Answers 3

Reset to default 3

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)
});

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()');
发布评论

评论列表(0)

  1. 暂无评论