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

Comparison and example usage of throw and next methods in javascript

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

Share

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

This article mainly explains "the comparison and example usage of throw and next methods in javascript". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the comparison and example usage of throw and next methods in javascript".

1. The generator has a throw method, which has the same effect as next.

2. The only difference is that the parameters transmitted by the next method return to normal values. The parameter passed by the throw method is the wrong object.

And throw sets the iterator state to the end of the iteration.

Example

Function* generatorFunc () {console.log ('function start...') Let res = yield 1 console.log ('logger-1, res) res = yield 2 console.log (' logger-2, res) res = yield 3 console.log ('logger-3, res) return' function end...'} const generator = generatorFunc () generator.next () / execute until yield 1 statement stops / * * print: 'function start...'* returns: {value: 1 Done: false} * / / if you pass an error object generator.next (new Error ('error ~')) / / execute to yield 2 statement to stop / * print: 'logger-1' [error object ('error ~')] * returns: {value: 2, done: error} * / generator.throw (new Error ('error ~')) / / throw an error End of iteration / * print: [error object ('error ~')] * returns: nothing...*/// then call next () to return {value: undefined, done: true} so far. I believe you have a better understanding of "the comparison and example usage of throw and next methods in javascript". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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