async和promise的用法区别在哪里
文章目录
之前一直很迷惑这块,今天看到以下代码,我多少往前迈进一步。
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.
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论