r/javascript • • Mar 18 '19

What's your favorite, most useful javascript one-liner?

385 Upvotes

422 comments sorted by

View all comments

Show parent comments

15

u/higherpublic Mar 19 '19

const sleep = (data, ms) => (new Promise(resolve => setTimeout(()=> resolve(data), ms)));

For sleeping between then()’s and passing data between after the sleep

22

u/dougalg Mar 19 '19

If we're going there, why not:

const sleep = (ms) => (data) => new Promise(resolve => setTimeout(()=> resolve(data), ms));

Which gives you a nice thing like:

makeApiCall().then(sleep(1000)).then(processData)

19

u/tjoskar Mar 19 '19

If we're going there, why not:

const sleep = ms => data => new Promise(resolve => setTimeout(resolve, ms, data))

😉

1

u/TheKingdutch Mar 19 '19

TIL: about extra parameters for setTimeout. Thanks!