如果 Promise.all
解析后满足某些条件,我想从函数返回。但return语句似乎没有效果,函数继续执行。
这是我的代码:
function somefunc() {
Promise.all([match2, match3, match4, match5]).then(results => {
console.log(results);
if(results.indexOf(false) > -1) {
return; // has no effect. somefunc() does NOT return.
}
else {
// ............
};
}).catch(err => {
console.log(err);
});
// this continues to get executed even after the return staement
}
最佳答案
如果您希望返回 Promise 链中的某些内容,则必须返回 Promise.all:
function somefunc() {
return Promise.all([match2, match3, match4, match5]).then(results => {
console.log(results);
if(results.indexOf(false) > -1) {
return; // has an effect. somefunc() will return.
}
else {
// ............
};
}).catch(err => {
console.log(err);
});
// this will not get executed after the return staement
}
关于javascript - 从 Promise.all 返回不会从包装函数返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52074481/