一零零、Promise.resolve()详解

Promise.resolve等价于下面的写法


有时需要将现有对象转为 Promise 对象,Promise.resolve方法就起到这个作用。

Promise.resolve('foo')
// 等价于
new Promise(resolve => resolve('foo'))

Promise.resolve方法的参数分成四种情况。


参数是一个 Promise 实例

如果参数是 Promise 实例,那么Promise.resolve将不做任何修改、原封不动地返回这个实例。

参数是一个thenable对象

thenable对象指的是具有then方法的对象,比如下面这个对象

let thenable = {
  then: function(resolve, reject) {
    resolve(42);
  }
};

Promise.resolve方法会将这个对象转为 Promise 对象,然后就立即执行thenable对象的then方法。

let thenable = {
  then: function(resolve, reject) {
    resolve(42);
  }
};

let p1 = Promise.resolve(thenable);
p1.then(function(value) {
  console.log(value);  // 42
});

thenable对象的then方法执行后,对象p1的状态就变为resolved,从而立即执行最后那个then方法指定的回调函数,输出 42

参数不是具有then方法的对象,或根本就不是对象

如果参数是一个原始值,或者是一个不具有then方法的对象,则Promise.resolve方法返回一个新的 Promise 对象,状态为resolved。

const p = Promise.resolve('Hello');

p.then(function (s){
  console.log(s)
});
// Hello

由于字符串Hello不属于异步操作(判断方法是字符串对象不具有 then 方法),返回 Promise 实例的状态从一生成就是resolved,所以回调函数会执行。Promise.resolve方法的参数,会同时传给回调函数

不带有任何参数

Promise.resolve方法允许调用时不带参数,直接返回一个resolved状态的 Promise 对象。

setTimeout(function () {
  console.log('three');
}, 0);

Promise.resolve().then(function () {
  console.log('two');
});

console.log('one');

// one
// two
// three

.then()函数里不返回值或者返回的不是promise,那么 then 返回的 Promise 将会成为接受状态(resolve)

Promise.resolve().then(() => console.log(2)).then(() => console.log(3));
console.log(1); // 1, 2, 3

需要注意的是,立即resolve的 Promise 对象,是在本轮“事件循环”(event loop)的结束时执行执行,不是马上执行,也不是在下一轮“事件循环”的开始时执行
原因:传递到 then() 中的函数被置入了一个微任务队列,而不是立即执行,这意味着它是在 JavaScript 事件队列的所有运行时结束了,事件队列被清空之后,才开始执行

resolve()本质作用


  • resolve()是用来表示promise的状态为fullfilled,相当于只是定义了一个有状态的Promise,但是并没有调用它;
  • promise调用then的前提是promise的状态为fullfilled
  • 只有promise调用then的时候,then里面的函数才会被推入微任务中;

最后推荐一个 js常用的utils合集,帮我点个star吧~

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Promise.resolve 是一个在 JavaScript 中用来创建一个已经解决(resolved)的 Promise 对象的方法。它返回一个表示解决结果的 Promise 对象。 当传递给 Promise.resolve参数是一个包含 then 方法的对象时(称为 thenable 对象),Promise.resolve 方法会将该对象转换为一个 Promise 对象,并将其解决结果作为该 Promise 对象的解决值。如果传递给 Promise.resolve参数已经是一个 Promise 对象,则它会直接返回该对象,不做任何处理。 以下是 Promise.resolve 的示例用法: ```javascript // 创建一个已解决的 Promise 对象 const resolvedPromise = Promise.resolve("Resolved Value"); resolvedPromise.then(value => { console.log(value); // 输出 "Resolved Value" }); // 创建一个已解决的 Promise 对象,通过 thenable 对象 const thenable = { then(resolve) { resolve("Thenable Resolved Value"); } }; const resolvedThenable = Promise.resolve(thenable); resolvedThenable.then(value => { console.log(value); // 输出 "Thenable Resolved Value" }); // 返回传入的 Promise 对象 const existingPromise = new Promise(resolve => { resolve("Existing Promise Resolved Value"); }); const resolvedExistingPromise = Promise.resolve(existingPromise); resolvedExistingPromise.then(value => { console.log(value); // 输出 "Existing Promise Resolved Value" }); ``` 解释: Promise.resolve 方法的作用是将一个值转换为一个已解决的 Promise 对象。如果传入的值是一个 Promise 对象,那么 Promise.resolve 方法会直接返回该对象。如果传入的值是一个 thenable 对象,它会转换为一个 Promise 对象,并将其解决结果作为解决值。如果传入的值是一个非 thenable 对象,它会被包装在一个新的 Promise 对象中,并将其作为解决值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值