2017-10-12 49 views
0

我有一段代码写的lodash象下面这样:使用ramda处理的承诺,并等待

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

我想改变它ramda,后夫妇的思考和阅读一些文件我写下面的代码:

const resualt = 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" 

}

+0

'几个音符await'是关键字,不是一个可以组成的函数。你需要把它解析成'then'并明确地处理这些承诺。最好你可以使用['composeP'](http://ramdajs.com/docs/#composeP)。 – Bergi

+0

TBH,我不明白你为什么要使用'compose',没有什么需要改变表达式'await Promise.all(R.map(uOrder => uOrder.invoice,await order.upstreamOrders ))'。 – Bergi

回答

2

我觉得这是一个好的开始打破究竟原始功能是做

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,因为我们可以从其他函数中构建它。如果你打破它,我们所做的就是抢invoice,并在两个不同的地方amount,但我们可以只抓数量的阵列

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是咖喱(我不知道upstreamOrders是如何工作的,难道是返回一个承诺一个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), 
); 
+0

很好的答案,谢谢。作为你的意见,我应该如何完美地学习ramda?我编码与abotut 1个月,但仍然有麻烦! \ – amir

+0

不要担心完美学习 - 没有这种东西!有一件事可能有助于学习,但将问题分解成组成部分。在这种情况下,它将数据提取和转换分开,并分别测试'compose'中的每个函数并构建它们。当我回答时,我分析了ramdajs.com/repl/以获得快速反馈并专注于具体问题。 –

+0

我肯定会推荐熟悉这些文档,并且这两个wiki页面https://github.com/ramda/ramda/wiki/What-Function-Should-I-Use%3F https://github.com/ ramda/ramda /维基/食谱。如果你能够尝试并理解这些例子的工作原理,那更好!在ramda博客系列中的思考也很棒http:// randycoulman。com/blog/2016/05/24/think-in-ramda-getting-started/ 其中最重要的一件事情也是这些功能中的一部分是ramda的一部分,但总的来说,这些想法比ramda 。试用其他fp库,教程甚至语言! –