Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the capture errors of async and await in Javascript

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to understand the capture errors of async and await in Javascript". In daily operation, I believe many people have doubts about how to understand the capture errors of async and await in Javascript. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how to understand the capture errors of async and await in Javascript". Next, please follow the editor to study!

When async and await capture error and normal output, export default {name: 'HelloWorld', created () {this.init ()}, methods: {init () {let p1 = new Promise ((resolve) = > {setTimeout () = > {resolve (666)} 1000)}) async function run () {let res = await p1 console.log ('res', res)} run () / / normal call: log output 666},},} try catch catch error {{msg}}

For a guide and recipes on how to configure / customize this project, check out the vue-cli documentation.

Installed CLI Plugins babel eslint Essential Links Core Docs Forum Community Chat Twitter News Ecosystem vue-router vuex vue-devtools vue-loader awesome-vue export default {name: 'HelloWorld', props: {msg: String,}, created () {this.init ()} Methods: {init () {let p1 = new Promise ((reject) = > {setTimeout () = > {reject (new Error ('wrong'))}, 1000)}) async function run () {try {let res = await p1 console.log ('res', res)} catch (error) {console.log (' error') Error)}} run () / / report an error when running res Error: error,} when multiple asynchronous nesting

There is no try {} catch {} to catch the wrong

Export default {name: 'HelloWorld', props: {msg: String,}, created () {this.init ()}, methods: {init () {let p1 = new Promise ((resolve) = > {setTimeout () = > {/ / reject (new Error (' wrong')) resolve (1)} 1000)}) let p2 = function (arg) {return new Promise ((reject) = > {setTimeout () = > {console.log ('arg', arg) reject (new Error (' wrong')}, 1000)})} async function run () {const res1 = await p1 console.log ('res1') Res1) / / 1 const res2 = await p2 (res1) console.log ('res2', res2) / / res2 Error: wrong oh} run ()},},}

Use try {} catch {} to catch the wrong

HelloWorldexport default {name: 'HelloWorld', components: {}, data () {return {}}, created () {this.init ()}, methods: {init () {let p1 = new Promise ((resolve) = > {setTimeout (()) = > {/ / reject (new Error (' wrong')) resolve (1)} 1000)}) let p2 = function (arg) {return new Promise ((reject) = > {setTimeout () = > {console.log ('arg', arg) reject (new Error (' wrong'))} 1000)} async function run () {try {var res1 = await p1 console.log ('res1', res1)} catch (error) {return new Error (' error1', error)} try {const res2 = await p2 (res1) console.log ('res2') Res2)} catch (error) {return new Error ('error2', error)}} run (),},}

Effect.

Await-to-js

Await-to-js official website

Asynchronous nesting uses try, and the code is relatively unintelligent.

Special use of third-party npm package await-to-js

Purpose: an asynchronous waiting wrapper that can easily handle errors without try-catch

Download: yarn add await-to-js-S

Use

HelloWorldimport awaitTo from 'await-to-js'export default {name:' HelloWorld', components: {}, data () {return {}}, created () {this.init ()}, methods: {init () {let p1 = new Promise ((resolve) = > {setTimeout (()) = > {/ / reject (new Error ('wrong')) resolve (1)} 1000)}) let p2 = function (arg) {return new Promise ((reject) = > {setTimeout () = > {console.log ('arg', arg) reject (new Error (' wrong'))}, 1000)})} async function run () {const [err Res1] = await awaitTo (p1) if (err) throw new Error ('error 1') console.log ('res1', res1) const [err2, res2] = await awaitTo (p2 (res1)) if (err2) throw new Error (' error 2') console.log ('res2', res2)} run ()},},}

Effect:

At this point, the study on "how to understand the capture errors of async and await in Javascript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report