打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
JavaScript 在 Promise.then 方法里返回新的 Promise
userphoto

2022.08.15 四川

关注

看一个实际的例子:

loadScript("/article/promise-chaining/one.js")
  .then(function(script) {
 return loadScript("/article/promise-chaining/two.js");
  })
  .then(function(script) {
 return loadScript("/article/promise-chaining/three.js");
  })
  .then(function(script) {
 // use functions declared in scripts // to show that they indeed loaded one();
 two();
 three();
  });

按照先后顺序,依次加载 one.js, two.js, three.js

当然也可以使用箭头函数的语法:

loadScript("/article/promise-chaining/one.js")
  .then(script => loadScript("/article/promise-chaining/two.js"))
  .then(script => loadScript("/article/promise-chaining/three.js"))
  .then(script => {
 // scripts are loaded, we can use functions declared there one();
 two();
 three();
  });

这里每个 loadScript 调用,都会返回一个 promise,这个新的 promise,在其被 resolve 时,会触发下一个 .then 的执行。

于是它开始加载下一个脚本。 所以脚本一个接一个地加载。

我们可以向链中添加更多异步操作。 请注意,代码仍然是 flat 的——它向下增长,而不是向右增长。

这种方式没有 pyramid of doom 的迹象。

虽然我们可以在 then 后面直接调用 loadScript:

loadScript("/article/promise-chaining/one.js").then(script1 => {
  loadScript("/article/promise-chaining/two.js").then(script2 => {
 loadScript("/article/promise-chaining/three.js").then(script3 => {
   // this function has access to variables script1, script2 and script3   one();
   two();
   three();
 });
  });});

此代码的作用相同:依次加载 3 个脚本。 但它 向右增长, 所以本质上仍然有回调地狱的问题。

Thenables 对象

准确地说,then 处理程序可能返回的不完全是一个 Promise,而是一个所谓的 thenable 对象——一个具有 .then 方法的任意 JavaScript 对象。 它将被视为与 Promise 相同的方式被处理。

这个想法是第 3 方库可以实现自己的 promise-compatible 对象。 它们可以有一组扩展的方法,但也可以与原生 Promise 兼容,因为它们实现了 .then。

看一个例子:

class Thenable {
  constructor(num) {
 this.num = num;
  }
  then(resolve, reject) {
 alert(resolve); // function() { native code } // resolve with this.num*2 after the 1 second setTimeout(() => resolve(this.num * 2), 1000); // (**)  }}new Promise(resolve => resolve(1))
  .then(result => {
 return new Thenable(result); // (*)  })
  .then(alert); // shows 2 after 1000ms

单步调试:

  1. 14 行 new Promise 内部的 executor 得到执行,立即 resolve,抛出 result 1

  1. 这会导致代码第 15 行 then 里的函数立即被调用,输入参数为 1,构造一个新的 Thenable 对象。

JavaScript 在第 16 行中检查 .then 处理程序返回的对象:如果它有一个名为 then 的可调用方法,那么它调用该 then 方法,并提供本地函数 resolve、reject 作为参数(类似于执行程序)并等待其中一个被调用。 在上面的示例中,resolve(2) 在 1 秒 (**) 后被调用。然后将结果进一步向下传递。

因此,下图第 16 行代码单步调试之后,会自动进入代码第 8 行。

  1. 打印出 resolve 原生函数的 native code 字符串。

  1. 代码第 10 行的 resolve,会触发第 18 行第二个 then 方法:

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
深入浏览器事件循环的本质
js之eventLoop,同步与异步,宏任务与微任务
ES6---Promise 4: 更多案例
介绍Promise构造函数的几种方法
AngularJs中的延迟加载
原以为很简单,结果这道 Promise 面试题让我失眠好一会
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服