Skip to content

Instantly share code, notes, and snippets.

@dgaubert
Created July 18, 2018 09:51
Show Gist options
  • Save dgaubert/9bf88df9ebafdad50a639c1eb9de9df0 to your computer and use it in GitHub Desktop.
Save dgaubert/9bf88df9ebafdad50a639c1eb9de9df0 to your computer and use it in GitHub Desktop.
const { promisify } = require('util')
function timeout (time, callback) {
if (time > 1000) {
throw new Error('Limit 1000')
}
if (time > 500) {
return callback(new Error('Limit 500'))
}
setTimeout(callback, time, null, true)
}
async function wait (time) {
return promisify(timeout)(time)
}
async function main () {
try {
await wait(1001)
} catch (err) {
console.error(err)
}
try {
await wait(501)
} catch (err) {
console.error(err)
}
try {
const ok = await wait(100)
console.log('OK', ok)
} catch (err) {
console.error(err)
}
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment