在 Promise.resolve 中使用了解构,其值为 thenable 对象?

is used destructuring in Promise.resolve with value thenable Object?

以下代码使用Destructuring:

var obj = {
  fun1:function() { console.log('Working') }
};

obj = { fun1:function(){ console.log("Replaced Working")} }

obj.fun1();

//output:
//Replaced Working

下面的Promise也是这样吗?

var pr = Promise.resolve({then:function(resolve){
  resolve(20);
  }
})


pr.then(function(v){
  console.log(v);
})

也就是说,pr等于Promise对象,其then方法改为:

function(resolve){
  resolve(20);
}

最后,pr.then(function(v){...} 产生 result.if 不使用 Destructuring ,所以为什么 then 属性 在 Promise.resolve

The following code use Destructuring

不,它没有。解构将是

 const { fun1 } = obj;
 fun1();

您只是在重写一个 属性。

Does the following Promise do the same?

不,它没有。 resolve 是 promise 构造函数的内部函数,然后调用传递给 then 的所有函数。完全没有属性重写

why there is passed then property in Promise.resolve?

我不知道,这完全没有意义。如果你这样做:

 const pr = Promise.resolve(42);
 pr.then(console.log) // 42

如果您传入一个对象,它将解析为该对象:

 const pr = Promise.resolve({ some: "thing" });
 pr.then(console.log); // { some: thing }

如果该对象具有 then 方法,则它被称为 thenable 对象,并且承诺将遵循该函数,假设它具有 function(onFulfill, onReject) 的签名。这就是你最后一个例子所做的。