LoginSignup
1
0

More than 1 year has passed since last update.

Denoでdelay()を使って待機処理

Posted at

Denoで待機処理するのん、どうするんかなと思って試してみた。
こんな感じ。
すごくシンプル。

import { delay } from "https://deno.land/std@0.161.0/async/mod.ts";

console.log(new Date());
await delay(1000);
console.log(new Date());

delay | /async/mod.ts | std@0.161.0 | Deno
Deno 標準ライブラリ async | Octo's blog

ちなみに、Nodeではこんな感じ。

import { setTimeout } from 'node:timers/promises'
await setTimeout(1000);

Node v.15から、setTimeout()はPromise返してくれるようになったらしい。
Timers | Node.js v19.0.0 Documentation
setTimeoutをawait/asyncで書く方法

ちなみに、ちなみに従来的なやり方はこんな感じ。

await new Promise((resolve) => setTimeout(resolve, 1000));

awaitできるsetTimeoutを1行で書く方法 - Qiita

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0