NodeJS 在并行块中处理异步函数并忽略单个项目的错误

编程入门 行业动态 更新时间:2024-10-03 12:30:48

NodeJS 在并行块中处理异步<a href=https://www.elefans.com/category/jswz/34/1771370.html style=函数并忽略单个项目的错误"/>

NodeJS 在并行块中处理异步函数并忽略单个项目的错误

我正在尝试以并行“块”的方式处理 NodeJS (v14) 中的异步函数。我的代码工作正常,除非异步函数 (

processItem()
) 其中一项出错,在这种情况下,整个处理被完全阻止。

const processGroup = async () => {
  const chunks = [
    [0, 1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10, 11],
    // etc.
  ];
  try {
    for (const chunk of chunks) {
      await Promise.all(chunk.map(async id => { await processItem(id) }));
    }
  } catch (e) {
    throw new Error('Error');
  }
}

const processItem = async id => {
  try {
    // run couple of async calls from another library (dependency)
    const itemURL = await itemProcessor.getItem(id);  // this might produce an error
    // ...
    // fetch item details
    const itemDetails = await fetch(itemURL);
  } catch (e) {
    console.log(e);
  }
}

如果ID为

0
的项目在
processItem()
函数中产生错误,似乎没有其他项目被处理,我也没有通过第一个块。在这种情况下,我怎样才能忽略错误的项目并继续处理其余的项目?

编辑
根据建议更新代码以使用

Promise.allSettled

const processGroup = async () => {
  const chunks = [
    [0, 1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10, 11],
    // etc.
  ];
  try {
    for (const chunk of chunks) {
      const results = await Promise.allSettled(chunk.map(id => processItem(id)));
      results.forEach((result, index) => {
        console.log(result, index);
        if (result.status === 'rejected') {
          console.error(`Error processing item with ID ${chunk[index]}: ${result.reason}`);
        }
      });
    }
  } catch (e) {
    throw new Error('Error');
  }
}

const processItem = async id => {
  console.log(id);
  try {
    // run couple of async calls from another library (dependency)
    const itemURL = await itemProcessor.getItem(id);  // this might produce an error
    // ...
    // fetch item details
    console.log(itemURL);
    const itemDetails = await fetch(itemURL);
  } catch (e) {
    console.log(e);
  }
}
回答如下:

如果您更新代码以使用

Promise.allSettled()
(而不是
Promise.all()
),则循环不会被任何失败或错误阻塞。然后,您可以选择以您认为最好的方式处理这些情况。

例如

const processGroup = async () => {
  const chunks = [
    [0, 1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10, 11],
    // etc.
  ];
  for (const chunk of chunks) {
    const results = await Promise.allSettled(chunk.map(id => processItem(id)));
    results.forEach((result, index) => {
      if (result.status === 'rejected') {
        console.error(`Error processing item with ID ${chunk[index]}: ${result.reason}`);
      }
    });
  }
};

const processItem = async id => {
  // run couple of async calls from another library (dependency)
  const itemURL = await itemProcessor.getItem(id);  // this might produce an error
  // ...
  // fetch item details
  const itemDetails = await fetch(itemURL);
};

更多推荐

NodeJS 在并行块中处理异步函数并忽略单个项目的错误

本文发布于:2024-05-30 04:48:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770165.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   错误   项目   NodeJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!