[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 ...
随机推荐
- python学习-58 configparse模块
configparse模块 1.生成文件 import configparser # 配置解析模块 config = configparser.ConfigParser() # config = { ...
- python使用ORM之如何调用多对多关系
在models.py中,我创建了两张表,他们分别是作者表和书籍表,且之间的关系是多对多. # 书 class Book(models.Model): id = models.AutoField(pri ...
- 【dfs】Sequence Decoding
Sequence Decoding 题目描述 The amino acids in proteins are classified into two types of elements, hydrop ...
- is 和 as 使用
类型判断 as: var newParser = parser as ClassA ; 转为ClassA类型赋值给newParser is: var flag = parser is ClassA ; ...
- vmware vSphere Data Protection 6.1--------2-初始化
一.简介 安装完vdp接下来就是部署初始化了 安装篇请参考:vmware vSphere Data Protection 6.1部署 二.开始初始化 登陆https://192.168.216.200 ...
- Python、PyCharm、Django框架安装
一.下载Python环境 1.1 下载Python环境,以下网址: https://www.python.org/downloads/release/python-373/ 下载安装包: 1.2点击安 ...
- c#基础知识梳理(一)
一.C#简介 C#是微软公司发布的一种面向对象的.运行于.NET Framework之上的高级程序设计语言.C#看起来与Java有着惊人的相似:它包括了诸如单一继承.接口.与Java几乎同样的语法和编 ...
- Go net/http 发送常见的 http 请求
使用 golang 中的 net/http 包来发送和接收 http 请求 开启 web server 先实现一个简单的 http server,用来接收请求 package main import ...
- 基于【 Docker】六 || 部署Harbor仓库
第一步:下载harbor二进制文件:https://github.com/goharbor/harbor/releases 第二步:安装 docker compose sudo curl -L htt ...
- VBA子程序(十六)
子程序(Sub Procedures,也叫子过程)与函数类似,但有一些差异. 子过程不需要有返回一个值,而函数可能会或可能不会有返回一个值. 子程序可以不用call关键字来调用. 子程序总是包含在Su ...