You can use the async keyword to create an asynchronous function, which returns a promise. Example Code const example = async () => { console.log("this is an example"); }; The try block is designed to handle potential errors, and the code inside the catch block will be executed in case an error occurs. Example Code try { const name = "freeCodeCamp"; name = "fCC"; } catch (err) { console.log(err); // TypeError: Assignment to constant variable. } The await keyword waits for a promise to resolve and returns the result. Example Code const example = async () => { const data = await fetch("https://example.com/api"); console.log(data); }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter