[Javascript] Run asynchronous functions in sequence using reduce
This can be handy if you have a rate limit on API requests or if you need to pass the result of each promise to the next one.
function fetchMessages(username) {
return fetch(`https://example.com/api/messages/${username}`)
.then(response => response.json());
}
function getUsername(person) {
return person.username;
}
async function chainedFetchMessages(p, username) {
// In this function, p is a promise. We wait for it to finish,
// then run fetchMessages().
const obj = await p;
const data = await fetchMessages(username);
return { ...obj, [username]: data};
}
const msgObj = peopleArr
.map(getUsername)
.reduce(chainedFetchMessages, Promise.resolve({}))
.then(console.log);
// ⦘ {glestrade: [ … ], mholmes: [ … ], iadler: [ … ]}
[Javascript] Run asynchronous functions in sequence using reduce的更多相关文章
- 在JavaScript函数式编程里使用Map和Reduce方法
所有人都谈论道workflows支持ECMAScript6里出现的令人吃惊的新特性,因此我们很容易忘掉ECMAScript5带给我们一些很棒的工具方法来支持在JavaScript里进行函数编程,这些工 ...
- [ES7] Handle Errors in Asynchronous Functions
This lesson shows how regular control flow statements such as try/catch blocks can be used to proper ...
- JavaScript ES6 Arrow Functions(箭头函数)
1. 介绍 第一眼看到ES6新增加的 arrow function 时,感觉非常像 lambda 表达式. 那么arrow function是干什么的呢?可以看作为匿名函数的简写方式. 如: var ...
- Eloquent JavaScript #05# higher-order functions
索引 Notes 高阶函数 forEach filter map reduce some findIndex 重写课本示例代码 Excercises Flattening Your own loop ...
- [译]PYTHON FUNCTIONS - MAP, FILTER, AND REDUCE
map, filter, and reduce Python提供了几个函数,使得能够进行函数式编程.这些函数都拥有方便的特性,他们可以能够很方便的用python编写. 函数式编程都是关于表达式的.我们 ...
- [Javascript] Compose multiple functions for new behavior in JavaScript
In this lesson you will create a utility function that allows you to quickly compose behavior of mul ...
- [Javascript] Refactoring: Polymorphic Functions
if-statements can add serious complexity and beg for refactoring. You can use polymorphic functions ...
- javaScript中 数组的新方法(reduce)
定义和用法 reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值. reduce() 可以作为一个高阶函数,用于函数的 compose. 注意: redu ...
- [Javascript Crocks] Compose Functions for Reusability with the Maybe Type
We can dot-chain our way to great success with an instance of Maybe, but there are probably cases wh ...
随机推荐
- EFCore 调试远程SqlServer数据库提示信号灯超时时间已到
背景 最近在使用EFCore去连接阿里云上面的数据库进行开发的时候,当自己在Debug模式下总是提示下面的报错信息,然后找了好久都没有解决,报错信息如下: an exception has been ...
- 【HC89S003F4开发板】 3串口调试
HC89S003F4开发板串口调试 使用资料自带的demo 主程序 /************************************系统初始化************************ ...
- 近期学习python的小问题及解决方案
①定义空的二维列表来读取放置文件的内容: 在python中定义二维数组 - woshare - 博客园https://www.cnblogs.com/woshare/p/5823303.html ②调 ...
- PB笔记之数据窗口添加虚拟列的方法
1.选择计算域控件: 2.输入公式 3.添加一个输入框作为列名,注意Name必须改为后缀为_t(PB固定识别_t)才可以绑定输入框和计算域作为虚拟列,虚拟列在最后一列时,有可能不能改变宽度,需往前挪才 ...
- AS3.0 位图翻转、旋转
/* * * *-------------------------* * | *** 位图翻转.旋转 *** | * *-------------------------* * * 作 者:fengz ...
- windows环境下如何搭建Consul+Ocelot
下面的是markdown格式的文档,懒得排版了,有兴趣的话可以去github上看,有源码 Github:https://github.com/yuchengao0721/Consul-Ocelot.g ...
- javascript -- 时间转换
function numFormat(num){ //时间处理 return ('00' + num).substr(-2); #处理 日期前面有0的情况}function timeFormat ...
- 一、hystrix如何集成在openfeign中使用
所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 HystrixInvocationHandler hystrix是开源的一个熔断组件,s ...
- 关于下载gitbash客户端
gitbash的安装包找不到了,最近又在准备新机器的环境,需要安装git客户端. https://www.git-scm.com/,网址找到了,但是下载非常缓慢.搜了一下,都是改host,但是改了也没 ...
- 树莓派手动设置静态IP和DNS方法
在使用树莓派的过程中,往往需要手动设置一个静态的IP地址,一来可以防止DHCP自动分配的IP变动,二来可提高树莓派的网络连接速度.查看官方文档 man dhcpcd.conf可知,需要配置静态IP的话 ...