Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Deno.delay as promise version of setTimeout #4052

Closed
ry opened this issue Feb 20, 2020 · 4 comments
Closed

Add Deno.delay as promise version of setTimeout #4052

ry opened this issue Feb 20, 2020 · 4 comments
Labels
good first issue Good for newcomers

Comments

@ry
Copy link
Member

ry commented Feb 20, 2020

async function delay(ms: number): Promise<void> {
  const promise = createResolvable();
  setTimeout(() => {
    promise.resolve();
  }, ms);
  return promise;
}
@ry ry added the good first issue Good for newcomers label Feb 20, 2020
@nayeemrmn
Copy link
Collaborator

Only slightly better than new Promise(r => setTimeout(r, ms)).

Is it okay to encourage something non-portable for a trivial high-level convenience? I don't know if this is a good idea, at least with what I understand to be Deno's current set of philosophies.

@hayd
Copy link
Contributor

hayd commented Feb 20, 2020

This is in std:

deno/std/util/async.ts

Lines 110 to 117 in a0d3b4e

// Delays the given milliseconds and resolves.
export function delay(ms: number): Promise<void> {
return new Promise((res): number =>
setTimeout((): void => {
res();
}, ms)
);
}

@ry
Copy link
Member Author

ry commented Feb 20, 2020

@nayeemrmn, yea you make a good point...

@UrielCh
Copy link

UrielCh commented Jun 27, 2024

The up to date version:

deno add @std/async
import { delay } from "@std/async/delay";

await delay(100); // waits for 100 milliseconds

JSR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants