How to throw an error in an async generator function in JavaScript ? Last Updated : 26 Jul, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we will try to understand how to throw an error in a synchronous (abbreviated as "async") generator function in JavaScript with the help of theoretical as well as coding examples. Let us first have a look into the following section which will show us the syntax for declaring an async generator function. Syntax: async function* function_name () { ... } We will learn about the declaration of the async generator function and know how to throw an error in an async generator function. Example 1: In this example, we will use the throw keyword for an error using an error object that will contain an error message passed in by the user as per requirement, and later we will catch that error using a catch statement. JavaScript <script> async function* generator() { throw new Error("Error thrown from an " + "async generator function....!!!"); } let iterator = generator(); iterator .next() .then((result) => console.log(result.value)) .catch((error) => console.log(error.message)); </script> Output: Error thrown from an async generator function....!!! Example 2: In this example, we will use the yield keyword by which we will catch the error in the latter part of our code. Further, we will declare a promise whose state is in rejected one containing the error message in the reject() method inside it and then we will catch it using a catch statement. JavaScript <script> async function* generator() { yield new Promise((resolve, reject) => { reject("Error thrown from an async " + "generator function....!!!"); }); } let iterator = generator(); iterator .next() .then((result) => console.log(result.value)) .catch((error) => console.log(error)); </script> Output: Error thrown from an async generator function....!!! Comment More infoAdvertise with us Next Article How to throw an error in an async generator function in JavaScript ? A amansingla Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions JavaScript-Statements Similar Reads How to Check a Function is a Generator Function or not using JavaScript ? Given an HTML document containing some JavaScript function and the task is to check whether the given function is generator function or not with the help of JavaScript. There are two examples to solve this problem which are discussed below: Example 1:In this example, we will use functionName.constru 2 min read How to create an Asynchronous function in Javascript? JavaScript is a single-threaded and synchronous language. The code is executed in order one at a time. But Javascript may appear to be asynchronous in some situations. Example: HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title 6 min read How to append new information and rethrowing errors in nested functions in JavaScript ? In this article, we will see how we may append new information and rethrow errors in nested functions in JavaScript with the help of certain theoretical explanations & understand them through the illustrations. This article is divided into 2 sections, i.e. in the 1st section, we'll understand ho 5 min read How to use await outside of an async function in JavaScript ? In this article, we will try to understand in what way or by how we may use await outside of an async function in JavaScript with the help of both theoretical explanations as well as coding examples. Let us first understand the following shown section in which all the syntaxes of declaring a promise 4 min read How to Catch an Error from External Library and Store it in a String in JavaScript ? When using an external library in our code, we may encounter errors. It is essential to catch these errors and handle them gracefully. In some cases, we may want to store the error message in a string for further processing or display it to the user. In this article, we will learn how to catch an er 3 min read Like