【问题标题】:JavaScript Promises and setTimeoutJavaScript Promise 和 setTimeout
【发布时间】:2016-04-08 03:02:28
【问题描述】:

我一直在玩 Promises,但我无法理解以下代码发生了什么:

function a() { 
    return new Promise(function (resolve, reject) { 
        resolve("hi from a!");
    });
}

function b() { 
    return new Promise(function (resolve, reject) { 
        setTimeout(function () { 
            resolve("hi from b!");
        }, 5000);
    });
}

function c() { 
    return new Promise(function (resolve, reject) { 
        setTimeout(function () { 
            resolve("hi from c!");
        }, 1000);
    });
}

a().then(function (resultFromA) {
    console.log("a() responded with: " + resultFromA);
    b();
}).then(function (resultFromB) { 
    console.log("b() responded with: " + resultFromB);
    c();
}).then(function (resultFromC) { 
    console.log("c() responded with: " + resultFromC);
});

我希望这会立即输出a() responded with: hi from a!,以及b() responded with: hi from b!c() responded with: hi from c! 在各自的setTimeout() 触发后。但是,我立即得到以下输出:

a() 回复:hi from a!

b() 回应:未定义

c() 回应:未定义

我在想.then() 会等待这些承诺,但事实并非如此。任何帮助将不胜感激。

【问题讨论】:

    标签: javascript es6-promise


    【解决方案1】:

    您需要在 then 处理程序中使用 return b()return c()

    then 仅将第一个 Promise 替换为随后的 Promise,该 Promise 从其回调中返回

    如果你的then 回调不是return 一个promise,那么then 会应用到原来的promise,它会立即执行而不管之前的then 回调的内容/结果。

    基本上...

    a().then(function () {
      b()
    }).then( # This "then" is chained off of a's promise
    

    反之:

    a().then(function () {
      return b()
    }).then( # This "then" is chained off of b's promise
    

    【讨论】:

    • 这个解释不对。 `then()` 函数总是返回一个新的 Promise。然而,在上面的例子中(b()),函数的返回值是undefined,它被包装在一个新的 Promise 中。看起来第二个 Promise 与 a() Promise 链接在一起,因为 .then() 函数的主体同步完成,所以 then 立即完成。
    【解决方案2】:

    您需要return promises 链接它们:

    a().then(function (resultFromA) {
      console.log("a() responded with: " + resultFromA);
      // return b() promise
      return b();
    }).then(function (resultFromB) { 
      console.log("b() responded with: " + resultFromB);
      // return c() promise
      return c();
    }).then(function (resultFromC) { 
      console.log("c() responded with: " + resultFromC);
    });
    

    【讨论】:

      【解决方案3】:

      您忘记从函数调用返回。 Javascript 函数不会隐式返回。

      function a() { 
          return new Promise(function (resolve, reject) { 
              resolve("hi from a!");
          });
      }
      
      function b() { 
          return new Promise(function (resolve, reject) { 
              setTimeout(function () { 
                  resolve("hi from b!");
              }, 5000);
          });
      }
      
      function c() { 
          return new Promise(function (resolve, reject) { 
              setTimeout(function () { 
                  resolve("hi from c!");
              }, 1000);
          });
      }
      
      a().then(function (resultFromA) {
          console.log("a() responded with: " + resultFromA);
          return b(); // return 
      }).then(function (resultFromB) { 
          console.log("b() responded with: " + resultFromB);
          return c(); // return
      }).then(function (resultFromC) { 
          console.log("c() responded with: " + resultFromC);
      });
      

      【讨论】:

      • 我的问题是,出于某种原因,我认为 then 等待本地范围内的所有 Promise 来解决,而实际上它只是获得 returned 的那个。我最初并不想做return b()return c()
      • "Javascript 函数不会隐式返回。"不完全正确。当主体是单个表达式时,箭头函数 do 隐式返回:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
      猜你喜欢
      • 2021-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-10
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多