如何区分JavaScript中的Promise、async和await三者之间的具体差异与用法?

2026-04-03 06:311阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计517个文字,预计阅读时间需要3分钟。

如何区分JavaScript中的Promise、async和await三者之间的具体差异与用法?

终于搞懂了Promise、async、await的区别和联系了。下面是代码示例和写法说明:

1. Promise的写法:javascriptfunction fetchData() { return new Promise((resolve, reject)=> { // 模拟异步操作 setTimeout(()=> { resolve('数据获取成功'); }, 1000); });}

fetchData().then(result=> { console.log(result);}).catch(error=> { console.error(error);});

2. async和await的写法:javascriptasync function fetchData() { try { const result=await new Promise((resolve, reject)=> { // 模拟异步操作 setTimeout(()=> { resolve('数据获取成功'); }, 1000); }); console.log(result); } catch (error) { console.error(error); }}

fetchData();

主要看第2种和第6种写法。第2种是async函数的写法,它使用await关键字等待Promise结果。第6种是Promise的典型写法,通过then和catch处理Promise的结果和错误。

终于把promise, async, await的区别和联系弄清楚了,看下面代码

写法1,2是promise的写法

如何区分JavaScript中的Promise、async和await三者之间的具体差异与用法?

写法6是async和await的写法

主要看第2种写法和第6中写法即可, 第2种写法是promise的典型写法,第6中写法是async, await的典型写法

// 以下三个请求依次执行 req1 = () => { return fetch("example.com/api/v1/get")} req2 = () => { return fetch("example.com/api/v1/post")} req3 = () => { return fetch("example.com/api/v1/delete")} //写法1 req1().then(res=>{ console.log("1: ",res) req2().then(res =>{ console.log("2: ",res) req3().then(res =>{ console.log("3: ",res) }) }) }) // 写法2 req1().then(res =>{ console.log("1: ", res) return req2() }) .then(res =>{ console.log("2: ", res) return req3() }) .then(res =>{ console.log("3: ", res) }) // 写法3 function f1(){ req1() req2() req3() } // 写法4 async function f2(){ await req1 await req2 await req3 } // 写法5 async function f3(){ req1().then(res => { console.log("1:", res) }) await f3_1() } async function f3_1(){ req1().then(res => { console.log("2:", res) }) await f3_2() } async function f3_2(){ req2().then(res=>{ console.log("3: ",res) }) } // 写法6 ff() async function ff(){ await req1_good() } async function req1_good(){ fetch("example.com/api/v1/get").then(res =>{ console.log("1: ",res) }) await req2_good() } async function req2_good() { fetch("example.com/api/v1/post").then(res =>{ console.log("2: ",res) }) await req3_good() } async function req3_good() { fetch("example.com/api/v1/delete").then(res => { console.log("3: ",res) }) }

  • 最外层的async函数调用之后立即返回了,但是async还是里面还是在逐层执行
  • await的作用是等待其修饰的函数内部的所有await函数都执行完毕,
  • 从最外层启动一个async函数相当于go一个协程,await func 也相当于go 一个协程,不同在于await = go + waitgroup
  • await比promise高明的地方在于,promise在then里面调用另一个promise时,不得不return另一个promise再then, 或者在then中回调,但是await完全不需要
  • async是为了异步,await是为了异步+阻塞,缺一不可

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注自由互联的更多内容!

本文共计517个文字,预计阅读时间需要3分钟。

如何区分JavaScript中的Promise、async和await三者之间的具体差异与用法?

终于搞懂了Promise、async、await的区别和联系了。下面是代码示例和写法说明:

1. Promise的写法:javascriptfunction fetchData() { return new Promise((resolve, reject)=> { // 模拟异步操作 setTimeout(()=> { resolve('数据获取成功'); }, 1000); });}

fetchData().then(result=> { console.log(result);}).catch(error=> { console.error(error);});

2. async和await的写法:javascriptasync function fetchData() { try { const result=await new Promise((resolve, reject)=> { // 模拟异步操作 setTimeout(()=> { resolve('数据获取成功'); }, 1000); }); console.log(result); } catch (error) { console.error(error); }}

fetchData();

主要看第2种和第6种写法。第2种是async函数的写法,它使用await关键字等待Promise结果。第6种是Promise的典型写法,通过then和catch处理Promise的结果和错误。

终于把promise, async, await的区别和联系弄清楚了,看下面代码

写法1,2是promise的写法

如何区分JavaScript中的Promise、async和await三者之间的具体差异与用法?

写法6是async和await的写法

主要看第2种写法和第6中写法即可, 第2种写法是promise的典型写法,第6中写法是async, await的典型写法

// 以下三个请求依次执行 req1 = () => { return fetch("example.com/api/v1/get")} req2 = () => { return fetch("example.com/api/v1/post")} req3 = () => { return fetch("example.com/api/v1/delete")} //写法1 req1().then(res=>{ console.log("1: ",res) req2().then(res =>{ console.log("2: ",res) req3().then(res =>{ console.log("3: ",res) }) }) }) // 写法2 req1().then(res =>{ console.log("1: ", res) return req2() }) .then(res =>{ console.log("2: ", res) return req3() }) .then(res =>{ console.log("3: ", res) }) // 写法3 function f1(){ req1() req2() req3() } // 写法4 async function f2(){ await req1 await req2 await req3 } // 写法5 async function f3(){ req1().then(res => { console.log("1:", res) }) await f3_1() } async function f3_1(){ req1().then(res => { console.log("2:", res) }) await f3_2() } async function f3_2(){ req2().then(res=>{ console.log("3: ",res) }) } // 写法6 ff() async function ff(){ await req1_good() } async function req1_good(){ fetch("example.com/api/v1/get").then(res =>{ console.log("1: ",res) }) await req2_good() } async function req2_good() { fetch("example.com/api/v1/post").then(res =>{ console.log("2: ",res) }) await req3_good() } async function req3_good() { fetch("example.com/api/v1/delete").then(res => { console.log("3: ",res) }) }

  • 最外层的async函数调用之后立即返回了,但是async还是里面还是在逐层执行
  • await的作用是等待其修饰的函数内部的所有await函数都执行完毕,
  • 从最外层启动一个async函数相当于go一个协程,await func 也相当于go 一个协程,不同在于await = go + waitgroup
  • await比promise高明的地方在于,promise在then里面调用另一个promise时,不得不return另一个promise再then, 或者在then中回调,但是await完全不需要
  • async是为了异步,await是为了异步+阻塞,缺一不可

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注自由互联的更多内容!