雖然沒有 HTTP Client 回傳 Future,但只要有 HTTP Client 能回傳 Promise,就可透過 attemptP
回傳 Future。
Version
Fluture 14.0.0
attemptP
import { create, env } from 'sanctuary'
import { attemptP, fork } from 'fluture'
import { env as flutureEnv } from 'fluture-sanctuary-types'
import { log, error } from 'wink-fp'
let { pipe } = create ({ checkTypes: true, env: env.concat (flutureEnv) })
pipe ([
attemptP,
fork (error) (log)
])(_ => Promise.resolve (1))
使用 pipe
組合 IIFE:
_ => Promise.resolve (1)
:一般回傳 Promise 的 functionattemptP
:將 Promise 轉成 Futurefork (error) (log)
:解開 Future
attemptP :: (Undefined -> Promise a b) -> Future a b
從回傳 Promise 的 function 取出 Future
Undefined -> Promise a b
:一般回傳 Promise 的 functionFuture a b
:回傳 Future
Conclusion
- Fluture 的 function 中以
P
結尾都跟 Promise 有關 - 有了
attemptP
就不用擔心沒 HTTP client 可用,只要該 client 能回傳 Promise 即可