An one line implementation of sleep function
in JavaScript with yield + generator.
Without any callbacks or promises.
Warning: this is an experiment and not safe for production!
One line, just copy & paste:
let d=(f,t=f(),r=t.next())=>r.done||setTimeout(d,r.value,f,t)
Just run this in your Chrome console (Chrome >= 35, Firefox >= 38, Edge >= 13).
let d=(f,t=f(),r=t.next())=>r.done||setTimeout(d,r.value,f,t) // <- source code
d(function *() { // <- wrapper
// <- magic
console.log('foo') // output 'foo'
yield 1000 // wait for 1 sec
console.log('bar') // output 'bar'
yield 1000 // wait for 1 sec
console.log('baz') // output 'baz'
})
You could rename the d
function to avoid conflicts.
Or just use it under a safe scope.
@quietshu https://github.com/quietshu/delay
MIT licensed.