Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pfftdammitchris/35b725721b7cb559030ac21315308332 to your computer and use it in GitHub Desktop.
Save pfftdammitchris/35b725721b7cb559030ac21315308332 to your computer and use it in GitHub Desktop.
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