怎么“睡觉"?Dart 程序 [英] How can I "sleep" a Dart program

查看:42
本文介绍了怎么“睡觉"?Dart 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢在我的 Dart 应用程序中模拟异步 Web 服务调用以进行测试.为了模拟这些模拟调用响应的随机性(可能是无序的),我想对我的模拟进行编程,使其在返回未来"之前等待(睡眠)一段时间.

I like to simulate an asynchronous web service call in my Dart application for testing. To simulate the randomness of these mock calls responding (possibly out of order) I'd like to program my mocks to wait (sleep) for a certain period of time before returning the 'Future'.

我该怎么做?

推荐答案

2019版:

await Future.delayed(Duration(seconds: 1));

在同步代码中

import 'dart:io';

sleep(Duration(seconds:1));

注意:这会阻塞整个进程(隔离),因此不会处理其他异步函数.它也无法在网络上使用,因为 Javascript 实际上是仅异步的.

Note: This blocks the entire process (isolate), so other async functions will not be processed. It's also not available on the web because Javascript is really async-only.

这篇关于怎么“睡觉"?Dart 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆