async和promise的用法区别在哪里

7次阅读

共计 424 个字符,预计需要花费 2 分钟才能阅读完成。

之前一直很迷惑这块,今天看到以下代码,我多少往前迈进一步。

An async function always returns a promise.

async function fn() {return 'hello';}
fn().then(console.log)
// hello

The function fn returns ‘hello’. Because we have used async, the return value ‘hello’ is wrapped in a promise (via Promise constructor).
Here’s an alternate representation without using async —

function fn() {return Promise.resolve('hello');
}
fn().then(console.log);
// hello

In this, we are manually returning a promise instead of using async.

正文完
 0