Comparison and example usage of throw and next methods in javascript
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!