小编pad*_*wbr的帖子

如何使用Task monad?(fp-ts)

import * as T from 'fp-ts/lib/Task'
import { pipe, flow } from 'fp-ts/lib/function'

const getHello: T.Task<string> = () => new Promise((resolve) => {
  resolve('hello')
})
Run Code Online (Sandbox Code Playgroud)

我理解它的目的Task以及为什么它很重要。问题是我真的不知道如何正确使用它或用它进行创作。

如果我只是打电话getHello(),它会给我Promise<pending>

console.log(getHello()) // returns Promise<pending>
Run Code Online (Sandbox Code Playgroud)

但是,如果我这样做:

const run = async () => {
  const hello = await getHello()
  console.log(hello) // prints 'hello'
}
Run Code Online (Sandbox Code Playgroud)

有用。

但是这个:

const waitAndGet = async () => {
  return await getHello()
}

console.log(waitAndGet()) // prints Promise<pending>
Run Code Online (Sandbox Code Playgroud)

没有。

而且,我怎样才能用它来作曲呢?就像这样:

const waitAndGet = async () => …
Run Code Online (Sandbox Code Playgroud)

functional-programming typescript fp-ts

2
推荐指数
1
解决办法
4193
查看次数

标签 统计

fp-ts ×1

functional-programming ×1

typescript ×1