在保护区管道的中间执行Fluture任务 [英] Execute Fluture task in middle of Sanctuary pipe

查看:55
本文介绍了在保护区管道的中间执行Fluture任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的管道:

S.pipe([
    getRequestFile,                  // not async
    S.chain(saveTemporary),          // not async
    S.chain(copyImageToPublicPath),  // async
    S.chain(copyFileToPath),         // async
    S.chain(someLoggingFunction),    // not async
    S.chain(sendResponse),           // not async
]);

此管道中间有2个异步函数. 我想await表示copyImageToPublicPath,然后await表示copyFileToPath,然后继续正常操作

There are 2 async functions in middle of this pipe. I want to await for copyImageToPublicPath and then await for copyFileToPath and then continue the normal flow

经过一番搜索,我发现有Future.tryP个函数可以执行async,但是如何在该管道的中间使用Fluture呢?

After some search I found that there is Future.tryP function for doing async but how can I use Fluture in middle of this pipe?

推荐答案

这是排列类型的问题.

让我们组成一些类型定义以供示例使用:

Let's make up some type definitions to use in an example:

foo :: String -> String
bar :: String -> Future Error String
baz :: String -> Array String

现在,让我们逐步创建程序...

Now, let create our program step by step…

//    program :: a -> a
const program = S.pipe ([
]);

//    program :: String -> String
const program = S.pipe ([
  foo,               // :: String
]);

//    program :: String -> Future Error String
const program = S.pipe ([
  foo,               // :: String
  bar,               // :: Future Error String
]);

//    program :: String -> Future Error (Array String)
const program = S.pipe ([
  foo,               // :: String
  bar,               // :: Future Error String
  S.map (baz),       // :: Future Error (Array String)
]);

要在Future a b值内的b上进行操作,请使用S.mapS.chain.

To operate on the b inside a Future a b value we use either S.map or S.chain.

S.map可能导致不必要的嵌套:

S.map can lead to unwanted nesting:

fut :: Future Error String

quux :: String -> Future Error Number

S.map (quux) (fut) :: Future Error (Future Error Number)

我们可以使用S.chain来避免这种嵌套:

We could use S.chain to avoid this nesting:

fut :: Future Error String

quux :: String -> Future Error Number

S.chain (quux) (fut) :: Future Error Number

考虑将S.map添加到某个计算中不会失败的操作可能会有所帮助,而S.chain添加一个可能会失败的计算.

It may be helpful to think of S.map adding to some computation an operation that cannot fail, whereas S.chain adds a computation that can fail.

这篇关于在保护区管道的中间执行Fluture任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆