在 Ramda 管道中使用 Promise.all
Using Promise.all inside a Ramda pipe
如何在 ramda 管道中使用 Promise.all
?我想我在这里遗漏了一些愚蠢的东西。
const pipeline: Function = R.pipe(
R.map((record) => record.body),
R.map(JSON.parse),
R.map(asyncFunction),
console.log
);
Returns 一组承诺(Promise.all
的输入)
[ Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> } ]
但是,如果我尝试等待这些承诺通过 Promise.all
解决,就像这样:
const pipeline: Function = R.pipe(
R.map((record) => record.body),
R.map(JSON.parse),
R.map(asyncFunction),
Promise.all,
R.andThen(useTheReturn)
);
我最终遇到类型错误,指出我正在尝试使用 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)"
]
}
您需要将 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)
);
演示:
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>
我写了一个名为 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
类似于 ramda 的管道,它将函数链接在一起,但在异步函数的情况下,它会在将值传递给下一个函数之前解析返回的 promise。
map
就像 ramda 的映射,但它只适用于异步函数。
如何在 ramda 管道中使用 Promise.all
?我想我在这里遗漏了一些愚蠢的东西。
const pipeline: Function = R.pipe(
R.map((record) => record.body),
R.map(JSON.parse),
R.map(asyncFunction),
console.log
);
Returns 一组承诺(Promise.all
的输入)
[ Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> } ]
但是,如果我尝试等待这些承诺通过 Promise.all
解决,就像这样:
const pipeline: Function = R.pipe(
R.map((record) => record.body),
R.map(JSON.parse),
R.map(asyncFunction),
Promise.all,
R.andThen(useTheReturn)
);
我最终遇到类型错误,指出我正在尝试使用 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)"
]
}
您需要将 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)
);
演示:
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>
我写了一个名为 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
类似于 ramda 的管道,它将函数链接在一起,但在异步函数的情况下,它会在将值传递给下一个函数之前解析返回的 promise。
map
就像 ramda 的映射,但它只适用于异步函数。