代码之家  ›  专栏  ›  技术社区  ›  CodingYoshi

那么返回值是如何工作的呢

  •  0
  • CodingYoshi  · 技术社区  · 7 年前

    考虑下面的代码:

    function doSomething() {
        var promise = $.ajax(//...);
        return promise;
    }
    

    让我们一起使用它 then :

    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); 
    });
    

    问题 当前位置最后一个问题是什么 data 参数是否包含响应?这是怎么回事?

    1 回复  |  直到 7 年前
        1
  •  1
  •   ic3b3rg    7 年前

    spec :

    处理函数的行为遵循一组特定的规则。如果一个处理函数返回另一个 悬而未决的 承诺对象,由返回的承诺的决议/拒绝 then 将与处理程序返回的承诺值相同。