Javascript 从承诺中回报。一切都不';t从包装函数返回
我想从函数返回,如果某个条件满足一次Javascript 从承诺中回报。一切都不';t从包装函数返回,javascript,node.js,promise,Javascript,Node.js,Promise,我想从函数返回,如果某个条件满足一次Promise.all解析。但是return语句似乎没有效果,函数继续执行 这是我的密码: function somefunc() { Promise.all([match2, match3, match4, match5]).then(results => { console.log(results); if(results.indexOf(false) > -1) { retur
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.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
}
您必须返回
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
}
Promise.all([
是解析异步的,因此//即使在返回状态之后,也会继续执行该操作将始终在中的代码运行之前执行
您必须使用wait
/async
:
async function somefunc() {
try {
var results = await Promise.all([match2, match3, match4, match5]);
if (results.indexOf(false) > -1) {
return;
} else {
// ............
};
} catch (err) {
console.log(err);
}
// code that is executed if return is not done
}
或者您需要移动//的代码,即使在then中的返回状态
之后,此操作仍将继续执行,并且您应该返回
函数中的承诺链,这样,如果有人调用somefunc
,则可以等待函数完成:
function somefunc() {
return 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 {
// ............
};
// code that is executed if return is not done
}).catch(err => {
console.log(err);
});
}
Promise.all([
是解析异步的,因此//即使在返回状态之后,也会继续执行该操作将始终在中的代码运行之前执行
您必须使用wait
/async
:
async function somefunc() {
try {
var results = await Promise.all([match2, match3, match4, match5]);
if (results.indexOf(false) > -1) {
return;
} else {
// ............
};
} catch (err) {
console.log(err);
}
// code that is executed if return is not done
}
或者您需要移动//的代码,即使在then中的返回状态
之后,此操作仍将继续执行,并且您应该返回
函数中的承诺链,这样,如果有人调用somefunc
,则可以等待函数完成:
function somefunc() {
return 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 {
// ............
};
// code that is executed if return is not done
}).catch(err => {
console.log(err);
});
}
我将返回
添加到承诺中。所有
但承诺后的代码会继续执行。我将返回
添加到承诺中。所有
但承诺后的代码会继续执行。