Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ES6 Promise.delay
module.exports = function(duration) {
return function(){
return new Promise(function(resolve, reject){
setTimeout(function(){
resolve();
}, duration)
});
};
};
// Usage:
var delayPromise = require("./delay-promise");
doThing()
.then(...)
.then(delayPromise(5000))
.then(...)
@joshkel

This comment has been minimized.

Copy link

joshkel commented May 26, 2016

A slight revision to support passing arguments through:

function delayPromise(duration) {
  return function(...args){
    return new Promise(function(resolve, reject){
      setTimeout(function(){
        resolve(...args);
      }, duration)
    });
  };
}
@robinpokorny

This comment has been minimized.

Copy link

robinpokorny commented Jun 13, 2016

Well written! I was looking for such copy 'n paste code.

BTW I like to call it sleep, as discussed here: https://esdiscuss.org/topic/promise-returning-delay-function

@Thaina

This comment has been minimized.

Copy link

Thaina commented Sep 27, 2016

I think it shorter if just

new Promise((resolve) => setTimeout(resolve,100));
@mceachen

This comment has been minimized.

Copy link

mceachen commented Oct 19, 2016

Or TypeScript, with generics:

function delay<T>(millis: number, value?: T): Promise<T> {
  return new Promise((resolve) => setTimeout(resolve(value), 100))
}
@KostyaEsmukov

This comment has been minimized.

Copy link

KostyaEsmukov commented Nov 2, 2016

@mceachen There's a small typo in your code: you must have forgotten to replace the 100 with the millis.

@shaoshing

This comment has been minimized.

Copy link

shaoshing commented Apr 25, 2017

let delay = (time) => (result) => new Promise(resolve => setTimeout(() => resolve(result), time));

Promise.resolve("Data")
    .then(delay(1000))
    .then(result => console.info(result));
@cwg999

This comment has been minimized.

Copy link

cwg999 commented Aug 8, 2017

Or ECMAScript 2017,

(async () =>{
  const delay = time => new Promise(res=>setTimeout(()=>res(),time));
  await delay(2000);
  console.log('Hotdog');
})();
@jesseschalken

This comment has been minimized.

Copy link

jesseschalken commented Jan 21, 2018

@mceachen In addition to what @KostyaEsmukov said, your TS version calls resolve immediately. resolve(value) should be () => { resolve(value) }.

@moneydance

This comment has been minimized.

Copy link

moneydance commented Apr 4, 2018

Heres a function that makes the promise take at least time milliseconds to resolve. Good if you have a really fast request but want to show a loading state for it.

const delay = (time, promise) =>
  Promise.all([
    promise,
    new Promise(resolve => setTimeout(resolve, time))
  ]).then(([response]) => response);
@PabloCorso

This comment has been minimized.

Copy link

PabloCorso commented Apr 14, 2018

A slight variation could be:

const delay = ms => new Promise(_ => setTimeout(_, ms));

// usage:
function sayWoof() {
  delay(1000).then(() => console.log("Woof"));
}

// ECMAScript 2017:
async function sayWoof() {
  await delay();
  console.log("Woof");
}
@toonvanvr

This comment has been minimized.

Copy link

toonvanvr commented Aug 23, 2018

There's no need for a timeout. It seems that the app.disableHardwareAcceleration() behaves like an unawaited function call. In my case, setTimeout with 0 delay works.

I believe this means that a disableHardwareAcceleration() is a queued task with no internal awaitable tasks, thus kind of atomic.
EDIT: has atomic behavior OR finishes so quickly (as parallel native code) that waiting for app.on('ready') always takes a longer time. This is hypothetical as I haven't checked the code. It's possible that the execution time varies depending on your hardware, resulting in the 0-delay to be an insufficient measure.

$ electron .
app.disableHardwareAcceleration()
app.on('ready', () => setTimeout(createWindow))
@berstend

This comment has been minimized.

Copy link

berstend commented Jan 28, 2019

As I find myself ending up on this very page approx. once a week (copy pasting @PabloCorso version), here's a Node.js specific alternative for my future self 😄

const delay = require("util").promisify(setTimeout)
@justsml

This comment has been minimized.

Copy link

justsml commented Feb 11, 2019

Hi @berstend - I don't think util.promisify does what you hoped.

It expects a function whose callback argument is last. setTimeout's callback method is first, and timeout in milliseconds is 2nd. (The rarely used context arg is 3rd.)

This necessitates a wrapper function, I use something similar to https://gist.github.com/joepie91/2664c85a744e6bd0629c#gistcomment-2555399 :

const delay = ms => new Promise(yea => setTimeout(yea, ms));

delay(1000)
  .then(() => console.log('waited 1000 secs'))
@jgerstle

This comment has been minimized.

Copy link

jgerstle commented Feb 12, 2019

@justml I'm fairly certain you are wrong as it's in nodejs's documentation

@nbouvrette

This comment has been minimized.

Copy link

nbouvrette commented Feb 17, 2019

@justml @jgerstle

I confirm that the following code works perfectly with Node.js:

const wait = require('util').promisify(setTimeout);

(async () => {
    console.log('Wait...');
    await wait(1000);
    console.log('...a second.');
})();
@berstend

This comment has been minimized.

Copy link

berstend commented Nov 13, 2019

Hello future self, here's the TypeScript version for your copy/pasting pleasure:

const delay = (ms: number) => new Promise(_ => setTimeout(_, ms))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.