DEV Community

Cover image for ES6 compatible sleep function
Adam K Dean
Adam K Dean

Posted on

ES6 compatible sleep function

While debugging locally, it can be hard to see how an application runs in the wild as the network has no delay. Quite often, you get around this using a sleep function.

Pre-ES6 generators, you might do this with a callback, maybe using a setTimeout that calls the callback.

function sleep(ms, callback) {
    setTimeout(callback, ms);
}
Enter fullscreen mode Exit fullscreen mode

With ES6 generators, where you want to yield sleep(1000) etc, you can't use callbacks. What you can do is return a function that takes a single parameter, done, which through closure has access to the parameter you want to pass in, ms. When the returned function is called by whatever cog under the hood calls the returned functions when yielding, your inner function will have access to the ms parameter you passed in, along with a callback that JS passes in, which when called, will continue on from where you yielded the sleep.

function sleep(ms) {
    return function(done) {
        setTimeout(done, ms);
    }
}

yield sleep(1000);
Enter fullscreen mode Exit fullscreen mode

This is now available on npmjs.org. To install:

npm install es6-sleep
Enter fullscreen mode Exit fullscreen mode

To use, let's say while inside some Koa middleware:

var sleep = require('es6-sleep');

app.use(function *() {
    // do something
    yield sleep(1000);
    // continue
});
Enter fullscreen mode Exit fullscreen mode

Hopefully that makes sense. It does in my head.

Top comments (0)