Created
March 4, 2021 19:21
-
-
Save gyandeeps/0793a9da625726f2f92b028f1c9cf2a2 to your computer and use it in GitHub Desktop.
async await
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
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