Javascript承诺.catch仍然调用最终variable.then

使用noderestifybluebird promises ,睡眠剥夺的头脑:

 let chain = Promise.try(() => { return AsyncCall(auth_token).then(result => { if (!result) throw new Error('AUTH_TOKEN_ERROR'); } else { return facebook_data; } }); }).catch((error) => { res.code(401).error(error.message); next(); }); chain.then((result) => { // Gets called even though we throw the error. }); 

我的问题是,我真的很喜欢chain.then() – “结束”承诺链(为了可读性)的方法,但即使.catch得到一个错误。 我意识到我可以设置最后的.then()到第一个链接块,但我想明白这一点。

我怎样才能保持相同的代码结构,但有一个.catch() – 事件结束承诺执行stream程?

catch假定你正在处理错误。 如果你不是,并希望在稍后的catch处理,重新抛出它像这样:

 .catch((error) => { res.code(401).error(error.message); next(); throw error; });