我在想以下是否为正常行为?何时 - 出现中间错误的意外承诺链
代码
var when = require('when'); // I'm using [email protected] node_modules/when
console.log("INIT");
when.promise(function(resolve, reject) {
return when.reject("false")
.then(function(ok) {
console.log("Step 1 - in then, ok = %s", ok);
return 'ok1';
}, function(err) {
console.log("Step 1.1 - in catch, err = %s", err);
return reject(err);
}).then(function(ok) {
console.log("Step 2 - in then, ok2 = %s", ok);
return resolve("done");
}).catch(function(err) {
console.log("Step 3 - in catch, err = %s", err);
return reject(err);
});
}).then(function(mainok) {
console.log("Step 9 - in main then, mainok = %s", mainok);
}).catch(function(err) {
console.log("Step 9 - in main catch, err = %s", err);
});
这是我收到的输出运行它
INIT
Step 1.1 - in catch, err = false
Step 2 - in then, ok2 = undefined
Step 9 - in main catch, err = false
当读API我期待这一步骤1.1将被调用,那么第9步而不是第2步。
这是一个错误还是我误读了API?
感谢您的提示!
请注意[反模式](https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns)(创建不必要的承诺时,您可以使用/返回已有的)。 – jfriend00
避免['Promise'构造函数反模式](http://stackoverflow.com/q/23803743/1048572)! – Bergi