點燈坊

新しいことを始めるのに、遅すぎる挑戰はない

タグ : Fluture

如何解開 Future Either Maybe ?

Future Either Maybe 為常見的 Monad Stack,如呼叫 API 回傳 Future,再使用回傳 Either 的 Function 判斷 API 回傳值,又使用回傳 Maybe 的 Function 運算,就會出現 Future Either Maybe。

Fluture 初體驗

Promise 很像 Monad 但又不是 Monad,Monad 應該統一使用 mapchain,但 Promise 卻使用自己的 then,而 Future 則符合 Fantasy Land 規格,用法與觀念也與 Maybe 與 Either 相同。

Fluture 整合 Sanctuary 的 Type Checking

由於 Fluture 遵循 Fantasy Land 規格,因此也可搭配 Sanctuary 使用,但由於 Sanctuary 不認識 Future,因此 Type Checking 會失敗,Fluture 已經提供 fluture-sanctuary-types,可使 Sanctuary 認識 Future。

使用 lift2 提升 Binary Function

一般我們所寫的 Function 都只能處理 ECMAScript 的原生 Type,而無法處理 Maybe、Either、Future 這類 Monadic Type,透過 lift2 可將普通 Binary Function 提升成能處理 Apply 的 Function。

當 Either 遇見 Future

當有 Function 回傳 Either,而後續的 Function 都在 Either 內執行時,卻又有 Function 回傳 Future,這就造成 Either 內有 Future 窘境,實務上該如何處理這種兩層 Monad 呢 ?