Javascript .some()函数使用async和wait始终返回true
我有一个JavaScript类,里面有一些Javascript .some()函数使用async和wait始终返回true,javascript,async-await,Javascript,Async Await,我有一个JavaScript类,里面有一些async函数 如果这个数组中有一个大于10的数字,我希望函数返回true,但是如果数组中没有大于10的数字,我希望它返回false 我遇到的问题是,无论数组中是否有大于10的数字,函数调用总是返回true class Blah { async doStuff() { const nums = [1, 2, 3, 4, 5, 6]; const asyncResult = await nums.some(async
async
函数
如果这个数组中有一个大于10的数字,我希望函数返回true,但是如果数组中没有大于10的数字,我希望它返回false
我遇到的问题是,无论数组中是否有大于10的数字,函数调用总是返回true
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
如果我从.some()
函数中删除async
,那么它似乎可以工作,但该函数必须是async
,因为我需要等待这个函数。这是允许的函数调用,这也是async
函数。问题:
您的some
处理程序是一个async
函数。异步函数总是返回承诺,这被认为是真实的。例如,!!新承诺(()=>{})==true
解决方案:
相反,您可以使用Promise.all
。迭代每个num,如果有任何num通过了条件,则解析为true返回承诺。如果Promise.all
完成(即,您已检查所有NUM),且返回的承诺尚未解决(即,所有NUM均未通过您的条件),则解决false(或拒绝)您的返回承诺
class废话{
多斯塔夫(努姆斯){
返回新承诺((解决、拒绝)=>{
让承诺=nums.map(异步num=>{
如果(num>10&&等待此操作。允许(num))
决心(正确);
});
承诺。所有(承诺)。然后(()=>解决(错误));
});
}
允许异步(num){
log(`${num}位于数组`)中;
返回true;
}
}
常数b=新的Blah();
b、 doStuff([1,2,3,4,5,6]),然后(value=>console.log(value));
b、 doStuff([1,2,3,4,5,20]),然后(value=>console.log(value))代码>。有些代码是同步的。当您的函数返回承诺时(就像所有异步函数一样),。有些
将其视为真实值。因此,结果是正确的
如果希望解决所有承诺,然后检查结果,可以这样做:
async doStuff() {
const nums = [1, 2, 3, 4, 5, 6];
const promises = nums.map(async num => {
if (num > 10) {
const allowed = await this.isAllowed(num);
if (allowed) {
return true;
}
}
});
const areAllowed = await Promise.all(promises);
const result = areAllowed.some(val => val);
console.log(result);
}
async doStuff() {
const nums = [1, 2, 3, 4, 5, 6];
const result = await new Promise(resolve => {
let notAllowedCount = 0;
nums.forEach(async num => {
if (num > 10) {
const allowed = await this.isAllowed(num);
if (allowed) {
// Found one that is allowed. Immediately resolve the outer promise
resolve(true);
return;
}
}
// This one isn't allowed
notAllowedCount++;
if (notAllowedCount === nums.length) {
// If we've reached the end, resolve to false. This will only happen
// if none of the earlier ones resolved it to true.
resolve(false);
}
});
});
console.log(result);
}
如果这些都是潜在的长期承诺,并且您希望能够在其中任何一个承诺变为现实时立即出手,您可以这样做:
async doStuff() {
const nums = [1, 2, 3, 4, 5, 6];
const promises = nums.map(async num => {
if (num > 10) {
const allowed = await this.isAllowed(num);
if (allowed) {
return true;
}
}
});
const areAllowed = await Promise.all(promises);
const result = areAllowed.some(val => val);
console.log(result);
}
async doStuff() {
const nums = [1, 2, 3, 4, 5, 6];
const result = await new Promise(resolve => {
let notAllowedCount = 0;
nums.forEach(async num => {
if (num > 10) {
const allowed = await this.isAllowed(num);
if (allowed) {
// Found one that is allowed. Immediately resolve the outer promise
resolve(true);
return;
}
}
// This one isn't allowed
notAllowedCount++;
if (notAllowedCount === nums.length) {
// If we've reached the end, resolve to false. This will only happen
// if none of the earlier ones resolved it to true.
resolve(false);
}
});
});
console.log(result);
}
类似的内容应该会有所帮助(运行代码段):
const isLGTen=async(n)=>{
等待新的承诺(resolve=>setTimeout(resolve,1000));
返回n>10;
}
const someLGTen=nums=>Promise
.全部(数字地图(isLGTen))
.然后(result=>result.some(bool=>bool));
(异步()=>{
常量数据=[1,2,3,4,5,11];
console.log(
`是“${data}”中的一些>10”?`,
等待一些数据,
);
})()
一个async
函数返回一个承诺,它是truth-y,因此。一些总是会通过。如果您需要并行解决一系列承诺,请使用Promise.all
。或者如果你想串联解决它们,可以使用一个普通的for of
循环。我有点明白了-我想我应该把承诺放在哪里。所有的都在上面的代码中@jonrsharpeconst asyncResult=等待承诺。所有(/*承诺数组*/)
。然后你可以做asyncResult。一些(/*谓词*/)
。尝试了这个方法,我得到的只是未处理的PromiserEjectionWarning:TypeError:true是不可编辑的
修复了,我忘记了将解析(false)
封装在lambda中。再试一次。我还将nums设置为param,这样代码片段就可以展示一个正确和错误的示例。