简体   繁体   中英

How to apply a function that returns a promise to each list item in Ramda

I have a list of names and each needs to do a database fetch from Firebase. The FireStore get function returns a promise.

How do I work that in to Ramda? I've tried ComposeP/pipeP but I need these to work in a loop.

Think I'm missing something obvious as I'm just getting in to functional programming.

Any help or pointers would be appreciated.

It's not quite clear to me what you're looking for, but at first glance I believe you simply need Promise.all . Here is a simple solution that mocks Firebase and composes Promise.all with the Firebase call. After a 1 second delay, it should log the correct values.

 const people = { barney: {name: 'Barney Rubble', id: 1}, betty: {name: 'Betty Rubble', id: 2}, fred: {name: 'Fred Flintstone', id: 3}, wilma: {name: 'Wilma Flintstone', id: 4} } const FakeFirestore = { get: name => new Promise(r => setTimeout(_ => r(people[name]), 1000)) } const getAll = R.compose(Promise.all.bind(Promise), R.map(FakeFirestore.get)) getAll(['betty', 'wilma']).then(R.map(console.log)) 
 <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script> 

But note that the Ramda calls are not particularly important here. getAll could just as well be written as const getAll = names => Promise.all(names.map(FakeFirestore.get)) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM