相当于BlueBird Promise.props的ES6承诺? [英] Equivalent of BlueBird Promise.props for ES6 Promises?

查看:183
本文介绍了相当于BlueBird Promise.props的ES6承诺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想等到Promise完成一个字的地图。 BlueBird有Promise.props来完成这个,但有一个干净的方法在常规的JavaScript中执行此操作吗?我想我可以创建一个包含单词和Promise的新对象,获取这些对象的Promise数组,然后调用Promise.all并将它们放在地图中,但它看起来有点过分。

I would like to wait for a map of word to Promise to finish. BlueBird has Promise.props which accomplishes this, but is there a clean way to do this in regular javascript? I think I can make a new object which houses both the word and the Promise, get an array of Promises of those objects, and then call Promise.all and put them in the map, but it seems like overkill.

推荐答案

如果您正在处理 Map ,其值为promises(或promises和non-promises的组合) - 并且您希望最终解析的值为地图所有价值已解决

If you are dealing with a Map with values that are promises (or a mix of promises and non-promises) - and you want the final resolved value to be a Map with all values resolved

const mapPromise = map => 
    Promise.all(Array.from(map.entries()).map(([key, value]) => Promise.resolve(value).then(value => ({key, value}))))
    .then(results => {
        const ret = new Map();
        results.forEach(({key, value}) => ret.set(key, value));
        return ret;
    });

尽管如此,我敢打赌有人有一种更流畅的方式来实现这一点,一些新的ES2015 +的东西仍然是对我而言:p

Although, I bet someone has a slicker way to do this, some of the new ES2015+ stuff is still new to me :p

这篇关于相当于BlueBird Promise.props的ES6承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆