javascript - 我可以在 forEach() 数组助手之后链接 .then() 吗?

标签 javascript asynchronous foreach callback

我可以做这样的事情吗?

const array = ["foo", "bar", "hello", "etc"];

array.forEach(item => process(item)).then(() => {
    //run after forEach is done processing the whole array
});

最佳答案

没有。 then 用于处理 promise 。 forEach 是同步的,因此您应该将处理后的项目保存在新数组中,然后立即执行后处理逻辑。

关于javascript - 我可以在 forEach() 数组助手之后链接 .then() 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53890506/

相关文章:

javascript - 如何在 javascript 或 JQuery 中模拟 "organic"按键?

C++ boost asio |同步写入到异步读取 |无法在第二次读取中接收到正确的数据

php - 调用和 "forget about"异步 mysqlnd INSERT 查询的最佳方法

ios - 来自线程的 NSURLConnection 同步请求与异步请求

excel - 如何加速 VBA 中的 For Each 循环?

php - Magento - 如何将无限 CMS 静态 block (带有某些 "Identifier")的结果返回到 CMS 页面

javascript - Firebase 不部署。错误的自签名证书

javascript - ReactJS 中箭头函数和无箭头函数的区别?

javascript - 引用和调用对象问题?

php - 在 foreach 循环参数中分解数组