Created
July 18, 2018 09:51
-
-
Save dgaubert/9bf88df9ebafdad50a639c1eb9de9df0 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
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