- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在 _.mapValues 中,我想获得一些具有一定延迟的修改值(例如来自数据库),但我遇到了问题:当我在同步模式下修改值时,一切都很好,当我尝试使用 promise 或回调它的工作时不正确(在第一种情况下我得到 Promise 对象,在第二种情况下:未定义的值)。这里有一些简化的例子,我如何重写 mapValues 中的代码来解决这个问题?
'use strict';
const _ = require('lodash');
const Promise = require('bluebird');
let obj = {
one: 1,
two: 2,
};
let increment = (value) => {
return value + 1;
};
let incrementProm = (value) => {
return Promise.resolve(value + 1);
};
let incrementCb = (value, cb) => {
let res = value + 1;
let err = null;
setTimeout(cb.bind(undefined, err, res), 100);
};
let t1 = _.mapValues(obj, (value) => {
return increment(value);
});
let t2 = _.mapValues(obj, (value) => {
return incrementProm(value);
});
let t3 = _.mapValues(obj, (value) => {
let temp;
incrementCb(value, (err, res) => {
temp = res;
});
return temp;
});
console.log('Sync res:');
console.log(t1);
console.log('Promise res:');
console.log(t2);
console.log('Callback res:');
console.log(t3);
最佳答案
您可以使用 bluebird 的 props()函数来解析所有带有 promise 的属性。
Promise.props(_.mapValues(obj, incrementProm))
.then(result => console.log(result));
var obj = {
one: 1,
two: 2
};
var incrementProm = value => Promise.resolve(value + 1);
Promise.props(_.mapValues(obj, incrementProm))
.then(result => console.log(result));
<script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.4.1/bluebird.js"></script>
关于javascript - lodash mapValues 异步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38125401/
我有一个 Scala 作业,到目前为止,一切顺利。除了这个之外,所有内容都可以编译: @transient val aggs = msgs.transform { rdd =>
以下代码导致最后一行出现 StackOverflowError。 object StackTest extends App{ @tailrec def incrementValues(acc:
mapValues 方法创建一个新的 Map,它通过应用给定的函数将查询结果修改为原始 Map。如果同一个值被查询两次,传递给 mapValues 的函数将被调用两次。 例如: case class
我试图理解 mapValues方法适用于 Calendar Heatmap 的以下代码. 首先,一个函数加载一个字典: private func readHeatmap() -> [String: I
我有一张像这样的 map ,其中包含数百万个条目: private final Map tops = new HashMap<>(); 我需要获取值列表,这可以通过调用 java.util.Map V
我正在尝试以最优雅的方式使用 lodash 来转换以下对象: var original = { email: { 0: 'user01@gmail.com', 1: 'user02
我正在努力链接 _.mapValues .这是代码 var result = _.mapValues(results[0], function(val, qname) { return (re
在 _.mapValues 中,我想获得一些具有一定延迟的修改值(例如来自数据库),但我遇到了问题:当我在同步模式下修改值时,一切都很好,当我尝试使用 promise 或回调它的工作时不正确(在第一种
在我正在做的一个项目中,我经常发现自己在做以下模式。 给定一个案例类 X(a: A, b: B, c:C, d: Int),以及这样一个 X 的列表 xs: List[X], 我想做的数据库等效于按
类似于: How to infer a typed array from a dynamic key array in typescript? 我希望键入一个通用对象,该对象接收任意键到查找值的映射,
我在下面有这个示例 RDD(下面称为 rdd)。数据集是 (String, Int) 的元组: (some | random | value, 10) (some | random | value,
我正在尝试使用 messagpack.write(map) 序列化 map .在使用 messagepack.read(byte[]) 进行反序列化期间我得到了MapValue .但我无法使用 Map
刚才我很惊讶地得知mapValues产生一个 View 。结果如下例所示: case class thing(id: Int) val rand = new java.util.Random val
引用他们的javadoc,KStream.transformValues需要额外的storeName参数,因此是有状态的,同时通过transformValues方法,我们可以利用ProccessorC
将 lodash _.mapValues 转换为 ES6 有哪些不同的方法。 代码: var fruits= { 'apple': { 'name': 'apple', 'number
我正在尝试使用 PHP 和 Google api 将数据推送到 firestore 数据库。 在我在网络上看到的文档和示例中,我可以在发送数据时使用 mapValue 和 arrayValue。 我使
我一直在寻找一种方法来轻松获得 mongoose js 的结果,只需编写如上所示的查询即可: Article.find().skip(data.skip) 如果我想添加另一个查询,只需输入查询即可:
我正在寻找 plyr::mapvalues 的可读替代品在 data.table . 例如,在 plyr::mapvalues , 如果我想更改 carb 的值在 mtcars至 type1, typ
plyr::mapvalues 可以根据字典重新编码向量,即现有值和替换值的两个匹配向量。 library(plyr) data <- c("a", "b", "c", "a") dict_old <
我编写了以下小的实用程序函数,该函数使我的代码易于阅读: mapValues(Map map, Function f) => map.map((k, v) => MapEntry(k, f(v)));
我是一名优秀的程序员,十分优秀!