Javascript 那么返回值是如何工作的呢
考虑以下代码:Javascript 那么返回值是如何工作的呢,javascript,jquery,Javascript,Jquery,考虑以下代码: function doSomething() { var promise = $.ajax(//...); return promise; } 让我们将其与一起使用,然后使用: doSomething().then(function(data) { return true; // this is passed to the next then }).then(function(data){ alert(data); // this will sh
function doSomething() {
var promise = $.ajax(//...);
return promise;
}
让我们将其与一起使用,然后使用:
doSomething().then(function(data) {
return true; // this is passed to the next then
}).then(function(data){
alert(data); // this will show true as expected
return doSomething(); // A promise object should be passed to the next then
}).then(function(data){
// I expected this to be the promise object but this is not a promise object.
// It is the response from the doSomething() AJAX call
alert(data);
});
问题:为什么最后一个数据
参数包含响应?这是如何工作的?来自:
处理函数的行为遵循一组特定的规则。如果处理程序函数返回另一个挂起的承诺对象,则返回的承诺的解决/拒绝将在处理程序返回的承诺的解决/拒绝之后进行。另外,then
返回的承诺值将与处理程序返回的承诺值相同
谢谢当我离开办公桌时,我记得昨晚读过这篇文章,我正要删除这个问题,但你抢先一步。我很快就会接受答案。