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的更多相关文章

  1. 在JavaScript函数式编程里使用Map和Reduce方法

    所有人都谈论道workflows支持ECMAScript6里出现的令人吃惊的新特性,因此我们很容易忘掉ECMAScript5带给我们一些很棒的工具方法来支持在JavaScript里进行函数编程,这些工 ...

  2. [ES7] Handle Errors in Asynchronous Functions

    This lesson shows how regular control flow statements such as try/catch blocks can be used to proper ...

  3. JavaScript ES6 Arrow Functions(箭头函数)

    1. 介绍 第一眼看到ES6新增加的 arrow function 时,感觉非常像 lambda 表达式. 那么arrow function是干什么的呢?可以看作为匿名函数的简写方式. 如: var ...

  4. Eloquent JavaScript #05# higher-order functions

    索引 Notes 高阶函数 forEach filter map reduce some findIndex 重写课本示例代码 Excercises Flattening Your own loop ...

  5. [译]PYTHON FUNCTIONS - MAP, FILTER, AND REDUCE

    map, filter, and reduce Python提供了几个函数,使得能够进行函数式编程.这些函数都拥有方便的特性,他们可以能够很方便的用python编写. 函数式编程都是关于表达式的.我们 ...

  6. [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 ...

  7. [Javascript] Refactoring: Polymorphic Functions

    if-statements can add serious complexity and beg for refactoring. You can use polymorphic functions ...

  8. javaScript中 数组的新方法(reduce)

    定义和用法 reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值. reduce() 可以作为一个高阶函数,用于函数的 compose. 注意: redu ...

  9. [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 ...

随机推荐

  1. 长乐培训Day5

    T1 圆圈舞蹈 题目 [题目描述] 熊大妈的奶牛在时针的带领下,围成了一个圈跳舞.由于没有严格的教育,奶牛们之间的间隔不一致. 奶牛想知道两只最远的奶牛到底隔了多远.奶牛A到B的距离为A顺时针走和逆时 ...

  2. shell从简单到脱坑

    1.计算1-100的和(seq 1 100 使用反引号括起来的比较坑) #!/bin/bash ` do sum=$[$i+$sum] done echo $sum 2.编写shell脚本,要求输入一 ...

  3. 17 SUMIF函数、countif函数、averagif函数

    情景 按买家求他们的消费各是多少. 可以考虑使用分类汇总来做,但这里我们使用函数sumif来做. SUMIF函数 格式:=SUMIF(条件列表,匹配条件,数据区) 该函数这样理解:按照匹配条件,从条件 ...

  4. TZOJ5201: 数字游戏

    #include<stdio.h> int main() { ,j=; scanf("%I64d %I64d %I64d",&n,&k,&t); ...

  5. Scratch(二)来不及解释了,马上开始编程游戏

    来来来,上一期你们都经过了”HelloWorld”神咒的加持,已入编程大门,我们今天就开始一边做游戏,一边熟悉Scratch. “我只是切出去抢了个红包,一回来就到了编程游戏的环节了?” 对,你没跑错 ...

  6. Hibernate配置流程

    操作数据库必须要设置数据库的连接属性: driver_class,url,username,password(hibernate.cfg.xml)     2. 编写对象跟表之间的映射关系(类名.hb ...

  7. 利用RabbitMQ实现分布式事务

    实现要点:1.构建本地消息表及定时任务,确保消息可靠发送:2.RabbitMQ可靠消费:3.redis保证幂等 两个服务:订单服务和消息服务 订单服务消息可靠发送 使用springboot构建项目,相 ...

  8. 关于utf8mb4的使用

    针对mysql数据库存储一些特殊字符或者emoji的字符,所需要的编码类型.实际上基于efcore框架的情况下,codefirst自动迁移生成的数据库的默认编码格式,就是utf8mb4,以前的时候记得 ...

  9. java常见排序算法

    今天去面试的时候又考了排序算法,排序这个东西,你以为你懂了,但是真正去写的时候才会发现好多细节自己都模棱两可,我写着写着就全都乱了,回来之后赶紧重新写一遍. (1)冒泡排序 public void b ...

  10. 删除静态页面的html

    function dellist(obj) { $(obj).parent().parent().remove(); }