Javascript Async Generator functionThe unique feature of async generator functions in javascript is their support to both await and yield. It returns an async iterator rather than a promise or iterator. The async generator functions are distinct from async functions and the generator functions in javascript. The next() function of an async iterator always returns a promise. The only difference between an async generator and a conventional generator is that an async generator's next() method returns a Promise. The "for await..." of the statement is used to iterate over an async generator. SyntaxThe following syntax shows an async generator in the JavaScript function. ExamplesThe following examples use the different methods and functions in javascript. Example 1 In this example, you use the throw keyword to an error using an error object. It will include a user-supplied error message, and then we will use a catch statement to correct the error. Output The image shows the javascript async generator's operation and its output. Example 2 In this example, we will pair the async generator with the yield keyword. Using a catch statement, we will first declare a promise that is denied and which has the error statement in the reject() method. Output The image shows the javascript async generator's operation and its output. Example 3 In this example, the async generator function displays the use of the yield and await keywords. Output The image shows JavaScript async generator's operation and its output. Example 4 The example shows the iterator value using the operator and function. The iterator shows data from start to end values of the async generator function. Output The image shows JavaScript async generator's operation and its output. ConclusionThe JavaScript async generator uses to operate multiple data. It is used in the promise, iterator, and other methods to manage complicated functionality in a simple way. Next TopicJavascript "for...of" loop |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India