提问者:小点点

为什么javascript不等待和forEach并执行下一行[重复]


当我在nodejs中创建api并尝试将mongoose返回计数推送到新创建的数组时,它不会等待forEach并执行json.res()并给出空响应。当我使用setTimeout()时,它会给出正确的结果。

let newcategories = [];
let service = 0;
const categories = await Category.find({}, '_id name');
categories.forEach(async (category) => {

service = await Service.count({category: category});

newcategories.push({ count:service });
console.log('newcategories is -- ', newcategories);

});  /* while executing this forEach it's not wait and execute res.json..*/


console.log('result --- ',result);
console.log('out newcategories is -- ', newcategories);
res.json({status: 200, data: newcategories});

共2个答案

匿名用户

您需要使用map而不是forEach来收集等待并等待它们完成。编辑:或者您可以使用,它非常整洁(谢谢其他ppl)!

null

const categories = ['a', 'b', 'c'];

function getNextCategory(oldCategory) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(String.fromCharCode(oldCategory.charCodeAt(0)+1));
    }, 1000);
  });
}

async function blah() {
  const categoryPromises = categories.map(getNextCategory);

  const nextCategories = await Promise.all(categoryPromises);

  console.log(nextCategories);
}

blah();

async function blah2() {
  const nextCategories = [];

  for (const category of categories) {
    nextCategories.push(await getNextCategory(category));
  };

  console.log(nextCategories);
}


blah2();

匿名用户

因此,问题在于标记函数在默认情况下将返回一个承诺,而方法并不关心回调函数的结果类型,它只是执行一个操作。

函数中,它将正确地响应并填充新的类别,但是类别上的循环将会消失很久。

您可以选择将语句转换为循环,也可以使用,然后使用

for..of循环如下所示

for (let category of categories) {
  service = await Service.count({category: category});

  newcategories.push({ count:service });
  console.log('newcategories is -- ', newcategories);
}

地图版本如下所示

await Promise.all( categories.map(async (category) => {
  service = await Service.count({category: category});

  newcategories.push({ count:service });
  console.log('newcategories is -- ', newcategories);
}));

第二个版本之所以能工作,是因为只有当所有承诺完成后,promise.all才会解析,并且映射将为每个类别返回一个可能未解析的承诺