我遇到了一些关于 javascript Promise的问题,尤其是堆叠链。
谁能向我解释这些不同实现之间的区别(如果有的话!)?
实施 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();
})
实施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();
})
实施3
var serverSidePromiseChain;
serverSidePromiseChain = async().then(function(response) {
console.log('1', response);
return response;
});
serverSidePromiseChain = serverSidePromiseChain.then(function(response) {
console.log('2', response);
return true;
})
serverSidePromiseChain = serverSidePromiseChain.then(function(response) {
console.log('3', response); // response expected to be 'true'
return async3();
})
serverSidePromiseChain = serverSidePromiseChain.then(function(response) {
console.log('4', response);
return async4();
})
链的一部分返回值(步骤 2 中的“真”)这一事实是否会改变行为?Promise是否要求所有返回值都是异步Promise以保持行为?