2017-10-12 54 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庫,教程甚至語言! –