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. 阿里云服务器安装mysql8遇到的问题

    1.提供一个离线mysql下载路径:http://mirrors.163.com/mysql/Downloads/MySQL-8.0/ 2.在线mysql8安装: 2.1 下载mysql: wget ...

  2. Hystrix【异常机制处理】

    在之前的老版本中,feign中是默认开启hystrix的,从新版本中默认已经关闭了,如果要通过FeignClient调用服务并开启hystrix的话,需要自定义开启,即:feign.hystrix.e ...

  3. ListView控件的理解——自洽理论

    写在前面的话: *标题中已经说明,是自洽理论.因此,有几率会有理解错误.但是,你不可以因此骂我. -我这个人经不起别人的批评,如果你批评我,我就,我就.... ## <第一行代码>读书笔记 ...

  4. 最简容器化动手小实践——再战flappybird

    <Flappy Bird>是一名越南开发者所开发的游戏,这款游戏的主要内容是帮助一只小鸟穿越水管的层层阻碍,玩家所需要的只是点击屏幕从而调整小鸟的高度.而令这款游戏与众不同的是,这款游戏的 ...

  5. django类视图as_view()方法解析

    使用视图函数时,django完成URL解析之后,会直接把request对象以及URL解析器捕获的参数(比如re_path中正则表达捕获的位置参数或关键字参数)丢给视图函数,但是在类视图中,这些参数不能 ...

  6. vijo 1456最小总代价

    题意:中文题... 题解:状态比较多,可以说是状压的基础题吧,我们定义dp[i][j],j为一个二进制数,每位0表示接触过该物品,1表示没有接触过;j表示当前物品在谁手上.递推的顺序注意一下就好 ac ...

  7. js的for循环中出现异步函数,回调引用的循环值总是最后一步的值?

    这几天跟着视频学习node.js,碰到很多的异步函数的问题,现在将for循环中出现的异步函数回调值的问题总结如下: 具体问题是关于遍历文件夹中的子文件夹的,for循环包裹异步函数的代码: for (v ...

  8. Jenkins 发邮件的Job

    Jenkins要做到构建失败的时候发送邮件,常规做法是加个全局的post failure,类似这样的代码 pipeline { agent any stages { stage('deploy') { ...

  9. C# mailKit 发邮件 简单代码

    public static async Task<bool> SendMailAsync22(string Name, string receive, string sender, str ...

  10. XPATH中text()和string()的使用区别

    <table style="WIDTH: 95.45%; BORDER-COLLAPSE: collapse; EMPTY-CELLS: show; MARGIN-LEFT: 4.55 ...