How does F#'s async really work?

前端 未结 6 1045
逝去的感伤
逝去的感伤 2020-12-23 17:01

I am trying to learn how async and let! work in F#. All the docs i\'ve read seem confusing. What\'s the point of running an async block with Async

6条回答
  •  生来不讨喜
    2020-12-23 17:28

    The idea behind let! and Async.RunSynchronously is that sometimes you have an asynchronous activity that you need the results of before you can continue. For example, the "download a web page" function may not have a synchronous equivalent, so you need some way to run it synchronously. Or if you have an Async.Parallel, you may have hundreds of tasks all happening concurrently, but you want them all to complete before continuing.

    As far as I can tell, the reason you would use Async.StartImmediate is that you have some computation that you need to run on the current thread (perhaps a UI thread) without blocking it. Does it use coroutines? I guess you could call it that, although there isn't a general coroutine mechanism in .Net.

    So why does Async.Parallel require a sequence of Async<'T>? Probably because it's a way of composing Async<'T> objects. You could easily create your own abstraction that works with just plain functions (or a combination of plain functions and Asyncs, but it would just be a convenience function.

提交回复
热议问题