Skip to content

Instantly share code, notes, and snippets.

@cyberrrnaut
Created April 28, 2024 04:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyberrrnaut/a3d473b216f12ca04e388a4cd7122e41 to your computer and use it in GitHub Desktop.
Save cyberrrnaut/a3d473b216f12ca04e388a4cd7122e41 to your computer and use it in GitHub Desktop.
asynchronous function
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