我有一个JavaScript类,async里面有一些函数。
如果在这个数组中有一个大于10的数字,我希望.some()函数返回true,但是如果数组不包含大于10的数字,我希望它返回false。
我遇到的问题是函数调用总是返回true数组中是否有大于10的数字。
class Blah {
async doStuff() {
const nums = [1, 2, 3, 4, 5, 6];
const asyncResult = await nums.some(async num => {
if (num > 10) {
const allowed = await this.isAllowed(num);
if (allowed) {
return true;
}
}
});
console.log(asyncResult);
}
async isAllowed(num) {
console.log(`${num} is in the array`);
return true;
}}const b = new Blah();b.doStuff(); // This return true but it should be returning false这当前返回,true但正如您所看到的,数组没有大于的数字10
如果我async从.some()函数内部删除它似乎工作,但该函数需要是async因为我需要await在this.isAllowed函数调用,这也是一个async函数。
慕容森
肥皂起泡泡
拉莫斯之舞
相关分类