20
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Dartでsleep/wait的なやつ

Posted at

1.9からasync/awaitが使えるのでシンプルに書けるようになった。素敵。

import "dart:async";
main async {
  print("BEFORE SLEEP");
  await new Future.delayed(new Duration(seconds: 3));
  print("COMPLETE!");
}

これを実行すると(テキストじゃ伝わりにくいけど)

BEFORE SLEEP
...この状態で3秒まって...
COMPLETE!

みたいに書ける。素晴らしい。

  • dart:asyncimport されてること
  • 実行箇所は async が宣言されてること
  • new Future.delayed() の前に await を書くこと

この3つを守れば大丈夫。素敵。

ちなみにこれまでの書き方だと

import "dart:async";
main {
  print("BEFORE SLEEP");
  new Future.delayed(new Duration(seconds: 3)).then((_)=> print("COMPLETE!"));
}

みたいにネストせざるをえないので、随分わかりやすく書けるようになったね。よかったね。

20
10
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
20
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?