乐闻世界logo
搜索文章和话题

Is Promise.resolve a asynchronous function?

1个答案

1

Promise.resolve() is not an asynchronous function, but it can be used for asynchronous operations. The Promise.resolve(value) method returns a Promise object that is resolved with the given value. If the value is a promise, the new promise will inherit its state; if the value is thenable (i.e., has a 'then' method), the returned promise will resolve the thenable and fulfill with its final state; otherwise, the returned promise will be fulfilled with this value.

This method itself is synchronous; it immediately returns a Promise object. However, the design of the Promise constructor is for handling asynchronous operations, even though Promise.resolve() is synchronous, it processes callback functions asynchronously. For example:

javascript
console.log('1'); Promise.resolve().then(() => { console.log('2'); }); console.log('3');

In the above code, the output order will be:

shell
1 3 2
2024年6月29日 12:07 回复

你的答案