asynchronous - 如何使用 CancelableOperation 取消未来的链接
问题描述
我无法理解 dart async CancelableOperation.fromFuture 的工作原理。它似乎取消了第一个未来,但 .then 总是被执行。
在使用套接字时,我需要取消连接操作以避免等待套接字超时。
final op = f1
.then((_) => f2);
_connectOperation = CancelableOperation.fromFuture(op,
onCancel: () => throw CustomException());
return _connectOperation.valueOrCancellation();
f1 被取消,但 f2 仍被执行。
解决方案
内部发生的任何事情op
都不会受到任何影响。Future 没有用于向其传达取消意图的接口。
仅成功操作可应用于_connectOperation
.
_connectOperation = CancelableOperation.fromFuture(f1)
.then(
(value) => f2, // only runs if not cancelled
onCancel: () => throw CustomException()
);
推荐阅读
- microsoft-graph-api - 无法通过 Graph API 主动安装 Microsoft 团队应用
- android - 如何让图片出现在列表中
- html - 如何放置居中元素?
- php - 字符串长度验证php表单
- python - 实现k-means聚类的Python代码,不适用于k值,超过4?
- ruby-on-rails - Rails 5对不同的表使用belongs_to和条件
- python - 如何将代码从 python 复制到 GitHub?
- python - Python - Kivy:使用 ScreenManager 进行文本输入的 KeyError
- python - docker-compose 容器端口未显示在本地主机上
- python - csv 数据到数组中的多个字典