2017-04-25 48 views
1

我寫了一個函數,它返回一個promise列表(在ramda中的代碼),然後我必須用Promise.all()來解決所有的promise並將其發回給承諾鏈。將Promise.all([promise of list])轉換爲ramda

例如,

// Returns Promise.all that contains list of promises. For each endpoint we get the data from a promised fn getData(). 
const getInfos = curry((endpoints) => Promise.all(
    pipe(
    map(getData()) 
)(endpoints)) 
); 

getEndpoints() //Get the list of endpoints, Returns Promise 
    .then(getInfos) //Get Info from all the endpoints 
    .then(resp => console.log(JSON.stringify(resp))) //This will contain a list of responses from each endpoint 

promiseFn是返回Promise的函數。

我該如何最好地將此函數轉換爲完整的Ramda,並使用pipeP或其他東西?有人可以推薦嗎?

+1

你能否更詳細地解釋這是什麼一樣,也許有更明確的變量名?例如,外部'pipe'做什麼?它看起來不相關。 'promiseFn'做什麼? –

+0

@ScottSauyet我已經更新了這個問題,如果您需要更多信息,請告訴我。謝謝 – geek

+0

我不知道你的意思是「*打破使用咖喱函數的想法*」 – Bergi

回答

0

不知道你想要什麼來實現的,但我把它改寫這樣的:

const getInfos = promise => promise.then(
    endpoints => Promise.all(
    map(getData(), endpoints) 
) 
); 

const log = promise => promise.then(forEach(
    resp => console.log(JSON.stringify(resp)) 
)); 

const doStuff = pipe(
    getEndpoints, 
    getInfos, 
    log 
); 

doStuff(); 
相關問題