Skip to content

Instantly share code, notes, and snippets.

@gyandeeps
Created March 4, 2021 19:21
Show Gist options
  • Save gyandeeps/0793a9da625726f2f92b028f1c9cf2a2 to your computer and use it in GitHub Desktop.
Save gyandeeps/0793a9da625726f2f92b028f1c9cf2a2 to your computer and use it in GitHub Desktop.
async await
const delay = (data) => new Promise((resolve) => setTimeout(resolve.bind(null, data), 100))
const delayFail = (data) => new Promise((resolve, reject) => setTimeout(reject.bind(null, data), 100))
const asyncFunc = async (x = 0) => {
console.log(await delay(x + 22));
console.log(await delay(x + 21));
console.log(await delayFail(x + 20));
return 11;
}
const asyncAwait = async (func, ...args) => {
try {
const response = await func(...args);
return {response, error: null}
} catch (err) {
return {response: null, error: err}
}
}
const runner = async () => {
const {response, error} = await asyncAwait(asyncFunc, 1);
console.log(response);
console.error(error)
}
runner();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment