我想知道下面两个有区别吗?
aPromiseObj.then(fn1).then(fn2).catch(fn3);aPromiseObj.then(fn1); aPromiseObj.then(fn2); aPromiseObj.catch(fn3);工作流程会改变吗?
ps:我处于有条不紊的环境中,尽管我想从更广泛的角度来思考这个问题.
我正在一个nodejs项目中工作,并希望在链中跳过promise.以下是我的代码.在第一个promise块中,它将解析一个值{success: true}.在第二个块我要检查值success,如果为true我想将值返回给被调用者并跳过此链中的其余promises; 如果值为false,则继续链.我知道我可以抛出错误或拒绝它在第二个块,但我必须处理错误情况,这不是一个错误的情况.那么如何在承诺链中实现这一目标呢?我需要一个解决方案而不带任何其他第三方库.
new Promise((resolve, reject)=>{
resolve({success:true});
}).then((value)=>{
console.log('second block:', value);
if(value.success){
//skip the rest of promise in this chain and return the value to caller
return value;
}else{
//do something else and continue next promise
}
}).then((value)=>{
console.log('3rd block:', value);
});
Run Code Online (Sandbox Code Playgroud) 我一直遇到javascript承诺的几个问题,特别是堆叠链.
谁能向我解释这些不同实现之间的差异(如果有的话)?
实施1
var serverSidePromiseChain;
serverSidePromiseChain = async().then(function(response) {
console.log('1', response);
return response;
}).then(function(response) {
console.log('2', response);
return true;
}).then(function(response) {
console.log('3', response); // response expected to be 'true'
return async3();
}).then(function(response) {
console.log('4', response);
return async4();
})
Run Code Online (Sandbox Code Playgroud)
实施2
var serverSidePromiseChain;
serverSidePromiseChain = async().then(function(response) {
console.log('1', response);
return response;
});
serverSidePromiseChain.then(function(response) {
console.log('2', response);
return true;
})
serverSidePromiseChain.then(function(response) {
console.log('3', response); // response expected to be 'true'
return async3();
})
serverSidePromiseChain.then(function(response) {
console.log('4', response);
return async4();
})
Run Code Online (Sandbox Code Playgroud)
实施3
var serverSidePromiseChain; …Run Code Online (Sandbox Code Playgroud)