function fetchData(success) { return new Promise((resolve, reject) => { if (success) { resolve("Data fetched successfully!"); } else { reject("Error: Failed to fetch data."); } }); } async function getData() { try { const result = await fetchData(true); // Change to false to test rejection console.log(result); } catch (error) { console.error(error); } } getData();
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