Kri*_*ris 5 javascript promise es6-promise ramda.js
如何Promise.all在 ramda 管道中使用?我想我在这里错过了一些愚蠢的事情。
const pipeline: Function = R.pipe(
R.map((record) => record.body),
R.map(JSON.parse),
R.map(asyncFunction),
console.log
);
Run Code Online (Sandbox Code Playgroud)
返回一个承诺数组( 的输入Promise.all)
[ Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> } ]
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试等待这些承诺通过 解决Promise.all,如下所示:
const pipeline: Function = R.pipe(
R.map((record) => record.body),
R.map(JSON.parse),
R.map(asyncFunction),
Promise.all,
R.andThen(useTheReturn)
);
Run Code Online (Sandbox Code Playgroud)
我最终得到一个类型错误,指出我正在尝试用作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)"
]
}
Run Code Online (Sandbox Code Playgroud)
您需要绑定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)
);
Run Code Online (Sandbox Code Playgroud)
演示:
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)
);
Run Code Online (Sandbox Code Playgroud)
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)Run Code Online (Sandbox Code Playgroud)