为什么这个JavaScript Promise拒绝()在结果callback处理?
这里有更多代码的一个子集。 JavaScript中的Node.js 实质上,相当于函数调用了一些unit testing(在函数b
)。 从b
返回时,调用exceptiontesting(在函数c
)。 c
调用同步exceptiontesting(在函数d
)。 后来, c
将调用另一个函数(比如说)进行asynchronousexceptiontesting(Promise reject()的使用)。 在Node.js中使用Promise似乎是最好的,但即使使用它们也并不总是导致我预测的行为。
'use strict'; function d() { return new Promise(function(resolve, reject) { console.log('start d throw test'); try { throw new Error('Error type'); } catch (e) { console.log('d catch block e.message=' + e.message + ' rejecting to c'); return reject(new Error('d ' + e.message)); } // catch }) // Promise } function c() { return new Promise(function(resolve, reject) { console.log('start c'); d() .then( // d then function(result) { console.log('cd result callback'); }, function(error) { console.log('cd error callback error.message=' + error.message + ' rejecting to a'); return reject(new Error('second try')); } ) // d then }) // Promise } function b() { console.log('start b resolving to a'); return Promise.resolve(); } function a() { return new Promise(function(resolve, reject) { console.log('start a'); b() .then( // b then function(result) { console.log('ab result callback to c'); c(); }, function(error) { console.log('ab error callback error.message=' + error.message); } ) // b then .then( // c then function(result) { console.log('ac result callback '); }, function(error) { console.log('ac error callback error.message=' + error.message); } ) // c then .catch( function(error) { console.log('a final catch error.message=' + error.message); } ) // catch }) // Promise } a();
我预测,例如,每当我发出一个Promise reject(),处理将发生在调用者的错误callback。 (注意,每个reject()
也使用new Error
。)因此,我期望在console.log中输出。
start a start b resolving to a ab result callback to c start c start d throw test d catch block e.message=Error type rejecting to c cd error callback error.message=d Error type rejecting to a ac error callback error.message=second try
请注意,当d
调用reject()时,我预测处理将转到c
错误callback。 同样,一个c
reject()会去a
错误callback。 相反,我得到这个输出:
start a start b resolving to a ab result callback to c start c start d throw test d catch block e.message=Error type rejecting to c cd error callback error.message=d Error type rejecting to a ac result callback
c
reject()似乎正在进行结果callback。
函数b可能涉及; 如果我把它写出来的程序,我会得到所需的处理。 这对于这里很好,但是在更大的代码中,这是不行的。
问题:
- 为什么处理进行结果callback,而不是错误callback?
- functionb在完成后如何产生效果?
- 我怎样才能解决这个问题?
- 什么利弊与单独使用
return reject
与reject
挂钩? 大部分时间(至less)似乎都有效。 我还没有抓到造成问题的较短的forms。
基本问题是,你做了c();
而不是return c();
其他问题包括:
- 方法
d
,c
可以简化, 不要用new Promise(...)
- 避免使用
.then(success, error)
,大多数情况下,更好地使用then(success).catch(error)
。 -
你的代码可以缩减到下面:
'use strict'; function d() { return Promise.reject(new Error('Error type')); } function c() { console.log('start c'); return d() .then( result => console.log('cd result callback')) .catch( error => { console.log('cd error callback error.message=' + error.message + ' rejecting to a'); throw new Error('second try'); }); } function b() { console.log('start b resolving to a'); return Promise.resolve(); } function a() { console.log('start a'); return b() .then(result => { console.log('ab result callback to c'); return c(); }).catch(error => console.log('ab error callback error.message=' + error.message)) .then(result => console.log('ac result callback ')) .catch(error => console.log('a final catch error.message=' + error.message)); } a();