[英]How to use CancelableOperation to cancel future chaining
我無法理解dart異步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()
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.