javascript - 在 Array.find() 中使用异步函数
问题描述
看来我无法使用异步函数作为 Array.find() 的第一个参数。我不明白为什么这段代码不起作用,幕后发生了什么?
function returnsPromise() {
return new Promise(resolve => resolve("done"));
}
async function findThing() {
const promiseReturn = await returnsPromise();
return promiseReturn;
}
async function run() {
const arr = [1, 2];
const found = await arr.find(async thing => {
const ret = await findThing();
console.log("runs once", thing);
return false;
});
console.log("doesn't wait");
}
run();
解决方案
简单地说,find
不期望返回一个promise,因为它不是用于异步的东西。它循环遍历数组,直到其中一个元素导致返回真值。一个对象,包括一个 Promise 对象,是真实的,因此查找停止在第一个元素上。
如果你想要一个异步的 find 等价物,你需要自己写。您需要考虑的一个问题是您是要并行运行事物,还是要顺序运行它们,在继续下一个索引之前阻塞。
例如,这是一个并行运行它们的版本,然后一旦所有承诺都解决了,它就会找到第一个产生真值的。
async function findAsync(arr, asyncCallback) {
const promises = arr.map(asyncCallback);
const results = await Promise.all(promises);
const index = results.findIndex(result => result);
return arr[index];
}
//... to be used like:
findAsync(arr, async (thing) => {
const ret = await findThing();
return false;
})
推荐阅读
- java - 如何计算此代码的时间复杂度?
- google-kubernetes-engine - 在 Wordpress/GKE 安装中删除 Apache 服务器签名
- c# - 不考虑对 Microsoft Graph API 的两次连续调用之间的更改
- python - BeautifulSoup 获取元素之间的文本
- android - xamarin android mvvmcross 闪屏卡住了
- javascript - 获取用户在下载图片时给出的文件名
- sql - 如何根据长度拆分列
- python - 函数参数列表?
- php - 我想找到一种使用 Firebase 作为 Android、IOS 和 Web 应用程序后端的最佳方法
- python - 将 QColorDialog 添加到 QHBoxLayout