将嵌套 Promise 中的 catch 块放入内部以触发外部 Promise 的 catch 块,是否有其他更清洁的方法?
Throwing inside a catch block in a nested Promise to trigger the catch block of outer Promise, is there an alternative cleaner way?
我正在嵌套 Promise,我必须知道嵌套的 Promise 是被拒绝的 Promise 还是已完成的 Promise,才能知道是否触发外部 Promise 链的捕获。为了区分嵌套的 Promise 是被拒绝还是被实现,我在嵌套的 Promise 的 catch
中使用 throw
来表示拒绝;而当我嵌套的 Promise 的 catch
中没有 throw
时,总是会指示实现。请看下面的例子:
let test = new Promise((resolve, reject) => {
resolve(42);
}).then((e) => {
console.log(e);
return new Promise((resolve, reject) => { //Error happens inside this nested Promise
resolve(32);
}).then((e) => {
console.log(e);
//Run other processes that may lead to an error
}).catch((e) => { //I added this catch block for error detection, whatever error it might be
console.log(e);
throw(e); //I'm throwing (e) from the inner Promise's catch so that the returned Promise in the outer then is a rejected Promise, which will be "caught" by the catch block of the outer Promise
});
}).catch((e) => {
console.log(e); //I handle error that happen may either in the inner Promise or the outer Promise here
});
上面显示了我在嵌套 Promise 的 catch
块中 throw
-ing 的意思。以上是指示嵌套 Promise 失败的标准方法,还是有其他更清洁的方法来实现我想要的?如您所见,我实际上在嵌套的 Promise 中 throw
-ing 两次以表示拒绝。有没有一种方法可以让我 throw
一次并指示 Promise 拒绝?
编辑
我在内部 Promise 和 外部 Promise 中使用 catch
块的原因:我想检测内部 Promise 中的错误,并表示检测使用我的内部 catch
块完成;我想使用相同的处理程序处理内部 Promise 或外部 Promise 中可能发生的错误,这是使用我的外部 catch
块完成的。因为 catch
-ing 在我内部的 Promise return
中是一个 Promise,它被认为是对我外部 Promise 的 then
块实现的,我决定在我的内部 catch
中使用 throw
] 块以表明它实际上 没有实现 如果它到达内部 catch
块。我还编辑了我的代码,以表明我内部 Promise 中发生的错误 不是我在代码中 throw
手动触发的 。
我认为干净的方法是使用 async/await。但是在去那里之前,你的问题是当内部承诺失败时如何不 运行 外部承诺?
下例:
- 当内部承诺拒绝时,链条停止。
- 当外在的承诺被拒绝时,内在的承诺已经实现了。
const fun42 = () => {
return new Promise((resolve, reject) => {
setTimeout(() =>{
resolve(42)
reject('something at fun 42 went wrong')
}, 500)
})
}
const fun32 = () => {
return new Promise((resolve, reject) => {
setTimeout(() =>{
//resolve(32)
reject('something at fun 32 went wrong')
}, 500)
})
}
fun32().then(x => {
console.log(x)
return fun42()
}).then( y => {
console.log(y)
}).catch (e => {
console.log(e)
})
嗯,这是一个设计问题。错误处理应该发生在每个级别的一个地方(就像您在示例中所做的那样)。嵌套的 catch 函数处理错误,决定它是应该传播它还是安静地完成它(就像你写的那样)。
在那个具体的例子中,我会使用包装 Promise 的 reject
函数来拒绝它,而不是抛出错误。
我正在嵌套 Promise,我必须知道嵌套的 Promise 是被拒绝的 Promise 还是已完成的 Promise,才能知道是否触发外部 Promise 链的捕获。为了区分嵌套的 Promise 是被拒绝还是被实现,我在嵌套的 Promise 的 catch
中使用 throw
来表示拒绝;而当我嵌套的 Promise 的 catch
中没有 throw
时,总是会指示实现。请看下面的例子:
let test = new Promise((resolve, reject) => {
resolve(42);
}).then((e) => {
console.log(e);
return new Promise((resolve, reject) => { //Error happens inside this nested Promise
resolve(32);
}).then((e) => {
console.log(e);
//Run other processes that may lead to an error
}).catch((e) => { //I added this catch block for error detection, whatever error it might be
console.log(e);
throw(e); //I'm throwing (e) from the inner Promise's catch so that the returned Promise in the outer then is a rejected Promise, which will be "caught" by the catch block of the outer Promise
});
}).catch((e) => {
console.log(e); //I handle error that happen may either in the inner Promise or the outer Promise here
});
上面显示了我在嵌套 Promise 的 catch
块中 throw
-ing 的意思。以上是指示嵌套 Promise 失败的标准方法,还是有其他更清洁的方法来实现我想要的?如您所见,我实际上在嵌套的 Promise 中 throw
-ing 两次以表示拒绝。有没有一种方法可以让我 throw
一次并指示 Promise 拒绝?
编辑
我在内部 Promise 和 外部 Promise 中使用 catch
块的原因:我想检测内部 Promise 中的错误,并表示检测使用我的内部 catch
块完成;我想使用相同的处理程序处理内部 Promise 或外部 Promise 中可能发生的错误,这是使用我的外部 catch
块完成的。因为 catch
-ing 在我内部的 Promise return
中是一个 Promise,它被认为是对我外部 Promise 的 then
块实现的,我决定在我的内部 catch
中使用 throw
] 块以表明它实际上 没有实现 如果它到达内部 catch
块。我还编辑了我的代码,以表明我内部 Promise 中发生的错误 不是我在代码中 throw
手动触发的 。
我认为干净的方法是使用 async/await。但是在去那里之前,你的问题是当内部承诺失败时如何不 运行 外部承诺?
下例:
- 当内部承诺拒绝时,链条停止。
- 当外在的承诺被拒绝时,内在的承诺已经实现了。
const fun42 = () => {
return new Promise((resolve, reject) => {
setTimeout(() =>{
resolve(42)
reject('something at fun 42 went wrong')
}, 500)
})
}
const fun32 = () => {
return new Promise((resolve, reject) => {
setTimeout(() =>{
//resolve(32)
reject('something at fun 32 went wrong')
}, 500)
})
}
fun32().then(x => {
console.log(x)
return fun42()
}).then( y => {
console.log(y)
}).catch (e => {
console.log(e)
})
嗯,这是一个设计问题。错误处理应该发生在每个级别的一个地方(就像您在示例中所做的那样)。嵌套的 catch 函数处理错误,决定它是应该传播它还是安静地完成它(就像你写的那样)。
在那个具体的例子中,我会使用包装 Promise 的 reject
函数来拒绝它,而不是抛出错误。