DEV Community

Discussion on: Where’s the Sleep Function in JavaScript?

Collapse
 
morz profile image
Márton Papp

Great article! In NodeJS I usually prefer the one-liner version:

const sleep = require('util').promisify(setTimeout);
Collapse
 
erebos-manannan profile image
Erebos Manannán

The docs say it only works if the original function takes the callback as the final argument, setTimeout takes the callback as the first argument. Does this code even work? nodejs.org/dist/latest-v8.x/docs/a...

Collapse
 
danielescoz profile image
Daniel Escoz

Functions can define their own promisified version using a symbol, which is what setTimeout does in Node.js

So yes, it works.

Thread Thread
 
aumayeung profile image
John Au-Yeung

That makes sense. Good to know if we're using Node.js

Collapse
 
aumayeung profile image
John Au-Yeung

I think it got to be a Node.js style callback with err as the first parameter.