我有一个条件if
语句 who 的逻辑需要包裹在一个 Promise 中作为 if
之后的逻辑只能在它之后执行。
// WRAP 'if' statement below IN A PROMISE
if (a) { // could be true or false. If false, resolve the promise
// logic here
}
.then(
// execute logic after if here
)
我是 Nodejs 新手,正在尝试解决这个问题。我该如何解决这个问题?
最佳答案
只需将其包装到一个新的 Promise 中即可:
new Promise((resolve, reject) => {
if(a){
reject("error");
} else {
resolve(yourData);
}
})
.then(data => {
// Do stuff
})
.catch(err => {
// You should catch here an error rejected above
})
关于javascript - 如何将条件语句包装在 Promise 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53995390/