(1)参数是Promise实例,那么Promise.resolve将不做任何修改,原封不动的返回这个实例,不做任何处理

(2) 参数是thenable对象
thenable对象指的是具有then方法的对象,promise.resolve会将thenable对象转换成promise对象并立即执行thenable对象的then方法,thenable的then方法执行完,对象p1的状态变为resolve,从而执行最后的then方法指定的回调函数,输出42

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

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

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

resolve()本质作用
resolve()是用来表示promise的状态为fullfilled,相当于只是定义了一个有状态的Promise,但是并没有调用它;
promise调用then的前提是promise的状态为fullfilled;
只有promise调用then的时候,then里面的函数才会被推入微任务中;
原文链接:https://blog.csdn.net/my_new_way/article/details/104838192
网友评论