简体   繁体   English

在 Ramda 管道中使用 Promise.all

[英]Using Promise.all inside a Ramda pipe

How is Promise.all used in a ramda pipe? Promise.all如何在 ramda 管道中使用? I think I'm missing something silly here.我想我在这里错过了一些愚蠢的东西。

const pipeline: Function = R.pipe(
    R.map((record) => record.body),
    R.map(JSON.parse),
    R.map(asyncFunction),
    console.log
);

Returns an array of promises (the input to Promise.all )返回一个承诺数组( Promise.all的输入)

[ Promise { <pending> },
  Promise { <pending> },
  Promise { <pending> },
  Promise { <pending> },
  Promise { <pending> },
  Promise { <pending> },
  Promise { <pending> },
  Promise { <pending> },
  Promise { <pending> },
  Promise { <pending> } ]

However if I try to wait for those promises to resolve with Promise.all , like so:但是,如果我尝试使用Promise.all等待这些承诺解决,如下所示:

const pipeline: Function = R.pipe(
    R.map((record) => record.body),
    R.map(JSON.parse),
    R.map(asyncFunction),
    Promise.all,
    R.andThen(useTheReturn)
);

I end up with a type error stating that i'm trying to use Promise.all as a constructor type.我最终得到一个类型错误,指出我正在尝试使用Promise.all作为构造函数类型。

{
    "errorType": "TypeError",
    "errorMessage": "#<Object> is not a constructor",
    "stack": [
        "TypeError: #<Object> is not a constructor",
        "    at all (<anonymous>)",
        "    at /var/task/node_modules/ramda/src/internal/_pipe.js:3:14",
        "    at /var/task/node_modules/ramda/src/internal/_arity.js:11:19",
        "    at Runtime.exports.handler (/var/task/lambda/data-pipeline/1-call-summary-ingestion/index.js:14:19)",
        "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
    ]
}

You'll need to bind Promise.all to Promise :您需要将Promise.all绑定到Promise

const pipeline: Function = R.pipe(
  R.map((record) => record.body),
  R.map(JSON.parse),
  R.map(asyncFunction),
  R.bind(Promise.all, Promise),
  R.andThen(useTheReturn)
);

Demo:演示:

 const pipeline = R.pipe( R.bind(Promise.all, Promise), R.andThen(R.sum) ); const promise1 = Promise.resolve(10) const promise2 = 20 const promise3 = new Promise((resolve) => setTimeout(resolve, 100, 30)) const result = pipeline([promise1, promise2, promise3]) result.then(console.log)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>

I have written a library called rubico , which makes this simpler because you don't need to worry about binding Promise nor using Promise.all yourself.我编写了一个名为rubico的库,它使这变得更简单,因为您无需担心绑定 Promise 或自己使用 Promise.all。

import { pipe, map } from 'rubico'

const pipeline: Function = pipe([
  map((record) => record.body),
  map(JSON.parse),
  map(asyncFunction),
  console.log,
]);

pipe is similar to ramda's pipe in that it chains functions together, but in the case of async functions, it will resolve the returned promise before passing the value on to the next function. pipe类似于 ramda 的管道,因为它将函数链接在一起,但在异步函数的情况下,它将在将值传递给下一个函数之前解析返回的承诺。

map is like ramda's map, but it just works with async functions. map就像 ramda 的 map,但它只适用于异步函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM