Created
April 28, 2024 04:39
-
-
Save cyberrrnaut/a3d473b216f12ca04e388a4cd7122e41 to your computer and use it in GitHub Desktop.
asynchronous function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function myAsyncFunction() { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
const randomNumber = Math.random(); | |
if (randomNumber > 0.5) { | |
resolve(randomNumber); // Resolve the promise with a value | |
} else { | |
reject(new Error("Random number is less than 0.5")); // Reject the promise with an error | |
} | |
}, 1000); | |
}); | |
} | |
async function example() { | |
try { | |
const result = await myAsyncFunction(); | |
console.log("Promise resolved with result:", result); | |
} catch (error) { | |
console.error("Promise rejected with error:", error); | |
} | |
} | |
example(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment