简体   繁体   English

如何将返回诺言的函数应用于Ramda中的每个列表项

[英]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. 我有一个名称列表,每个名称都需要从Firebase提取数据库。 The FireStore get function returns a promise. FireStore的get函数返回一个promise。

How do I work that in to Ramda? 我该如何在Ramda中工作? I've tried ComposeP/pipeP but I need these to work in a loop. 我已经尝试过ComposeP / pipeP,但是我需要这些来循环工作。

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 . 我不清楚您要寻找的是什么,但是乍一看我相信您只需要Promise.all Here is a simple solution that mocks Firebase and composes Promise.all with the Firebase call. 这是一个Promise.all Firebase并通过Firebase调用组成Promise.all的简单解决方案。 After a 1 second delay, it should log the correct values. 延迟1秒后,它应该记录正确的值。

 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. 但是请注意,Ramda通话在这里并不是特别重要。 getAll could just as well be written as const getAll = names => Promise.all(names.map(FakeFirestore.get)) . getAll也可以写成const getAll = names => Promise.all(names.map(FakeFirestore.get))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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