Can I explicitly check for cancellation / terminate async computation?

穿精又带淫゛_ 提交于 2019-12-19 03:16:24

问题


I have an async computation like the following (see inline comments):

async {
  //...
  do! Async.Sleep(100) //cancellation may happen during sleep
  //... but isn't checked at the end of the sleep, so regular, non-async computations are executed here 
}

In order to force a cancellation check / terminate the entire async computation before the "regular" computation part is reached, I insert an effective no-op do! Async.Sleep(1) immediately after the do! Async.Sleep(100). Is there a cleaner way to do this? Possibly even something like do! Async.Nop.


回答1:


How about something like this:

let nop = async.Return ()

Then you could use it like:

async {
    // ...
    do! Async.Sleep 100
    do! nop
}


来源:https://stackoverflow.com/questions/18676657/can-i-explicitly-check-for-cancellation-terminate-async-computation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!