gpt4 book ai didi

javascript - 使用 Ramda 处理 Promise 和 Wait

转载 作者:行者123 更新时间:2023-11-28 14:44:59 25 4
gpt4 key购买 nike

我有一段使用 Lodash 编写的代码:

    const profit =
price -
_.sumBy(
await Promise.all(
map(uOrder => uOrder.invoice, await order.upstreamOrders),
),
'amount',
);

我想使用 Ramda 来更改它,在考虑并阅读一些文档后,我写了这个:

    const result = R.compose(
R.pick(['amount']),
await Promise.all(),
R.map(await order.upstreamOrders, uOrder.invoice),
);

当然这是错误的并且不起作用,但这是我的第一个方法,我想知道如何使用 Ramda 以功能性的方式完美地处理这样的情况。我该如何执行此操作?

对象的顺序也是以下示例的数组:

    { 
"_id" : ObjectId("59dce1f92d57920d3e62bdbc"),
"updatedAt" : ISODate("2017-10-10T15:06:34.111+0000"),
"createdAt" : ISODate("2017-10-10T15:06:33.996+0000"),
"_customer" : ObjectId("59dce1f92d57920d3e62bd44"),
"_distributor" : ObjectId("59dce1f92d57920d3e62bd39"),
"status" : "NEW",
"cart" : [
{
"count" : NumberInt(1),
"_item" : ObjectId("59dce1f92d57920d3e62bd57"),
"_id" : ObjectId("59dce1f92d57920d3e62bdc1")
},
{
"count" : NumberInt(1),
"_item" : ObjectId("59dce1f92d57920d3e62bd5c"),
"_id" : ObjectId("59dce1f92d57920d3e62bdc0")
},
{
"count" : NumberInt(1),
"_item" : ObjectId("59dce1f92d57920d3e62bd61"),
"_id" : ObjectId("59dce1f92d57920d3e62bdbf")
},
{
"count" : NumberInt(1),
"_item" : ObjectId("59dce1f92d57920d3e62bd66"),
"_id" : ObjectId("59dce1f92d57920d3e62bdbe")
},
{
"count" : NumberInt(1),
"_item" : ObjectId("59dce1f92d57920d3e62bd6b"),
"_id" : ObjectId("59dce1f92d57920d3e62bdbd")
}
],
"_upstreamOrders" : [
"4545643499"
],
"key" : "4592846350",
"__v" : NumberInt(1),
"_invoice" : "0811260909610702"
}

最佳答案

我认为这是一个很好的开始,可以分解原始函数到底在做什么

const profit =
price - // subtract the result
_.sumBy(
await Promise.all(
// Wait for upstreamOrders to resolve, and grab the 'invoice'
// key for each
map(uOrder => uOrder.invoice, await order.upstreamOrders),
),
// calculate the sum of the invoices, based on the 'amount' key
'amount',
);

考虑到这一点,我们可以分解这些步骤,并将计算(同步)与数据(异步)分开

Ramda 没有 sumBy,因为我们可以从其他函数组合它。如果你把它分解一下,我们所做的就是在两个不同的地方获取发票金额,但我们可以使用

获取一组金额
map(path(['invoice', 'amount']))

我们可以将其与 sumsubtract 一起删除,以创建一个完全独立于我们的异步代码的函数

const calculateProfits = (price, orders) => compose(
subtract(price),
sum,
map(path(['invoice', 'amount'])),
)(orders)

允许我们做类似的事情:

const profit = calculateProfits(price, await order.upstreamOrders)

或者如果 calculateProfits 被柯里化(Currying)了(我不确定upstreamOrders是如何工作的,它是一个返回promise的getter吗?)

const getUpstreamOrders = order => order.upstreamOrders

getUpstreamOrders(order)
.then(calculateProfits(price))
.then(profits => {
// Do what you want with the profits
})

最后,关于初次尝试的一些注意事项

const result = R.compose(
R.pick(['amount']),

// Promise.all is being called without any arguments
// it doesn't really fit within `compose` anyway, but if we were
// feeding an array of promises through, we'd want to just
// put `Promise.all,`
await Promise.all(),

// the arguments here should be the other way around, we're
// mapping over upstreamOrders, and the data comes last
// uOrder isn't available in this scope, previously it was
// `uOrder => uOrder.invoice`,
// invoking a function and returning the invoice for that order
R.map(await order.upstreamOrders, uOrder.invoice),
);

关于javascript - 使用 Ramda 处理 Promise 和 Wait,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46702691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com