Created
August 21, 2022 20:43
-
-
Save pfftdammitchris/35b725721b7cb559030ac21315308332 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 delay(ms, value) { | |
return new Promise((resolve) => { | |
setTimeout(() => resolve(value), ms) | |
}) | |
} | |
async function getRandomItem(arr) { | |
try { | |
const result = await delay(200, arr[Math.floor(Math.random() * arr.length)]) | |
if (typeof result === 'number') { | |
throw new Error(`Received a number`) | |
} | |
return result | |
} catch (error) { | |
throw error instanceof Error ? error : new Error(String(error)) | |
} | |
} | |
async function start() { | |
try { | |
return getRandomItem(['a', 1, 3, 'ccc']) | |
} catch (error) { | |
window.alert(`You picked out number. Please try again for a string`) | |
} | |
} | |
// The caller | |
start().catch((err) => { | |
console.error(`[${err.name}]: ${err.message}`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment