core.async 的重要性

在 Rich Hickey 的 talk 裡有提到 core.async 主要是用來解決兩大問題:

  1. Function chains make poor machine
  2. Real world concurrency is exposed via callback API

在 ES6 已經有了 C# async 的語法,不過,Rich Hickey 還是決定採用更有表達能力的 chan, alts, go-block 的語法。

如何與既有已經使用 callback handler 的系統接在一起

在 handler 裡,將資料塞進 channel 即可以。需要使用兩個特別的 API 。put!take! 這兩個 API 它們有兩個很重要的特性:

  1. 它們可以在 go-block 之外使用。 (>!<! 都必須在 go-block 內使用)
  2. 它們對 channel 的操作是 asynchronous 的。 ( >!<! 對 channel 都是 blocking 的。)

不使用 core.async 就很難寫出的程式

做多次相同的 query 請求,只取最快返回的那個結果,同時又有 timeout 的限制。

results matching ""

    No results matching ""