Somtime 'async await' can have a bad effect on code proferemence. Let's take a look the below example:

const fetch = require('node-fetch');
const BASE_URL = 'https://api.github.com/users'; class GithubUser {
async fetchGitHubUser(handle) {
const response = await fetch(`${BASE_URL}/${handle}`);
const body = await response.json(); if (response.status !== 200) {
throw Error(body.message);
} return body;
} async fetchGitHubRepos(handle) {
const response = await fetch(`${BASE_URL}/${handle}/repos`);
const body = await response.json(); if (response.status !== 200) {
throw Error(body.message);
} return body;
}
} (async () => {
const github = new GithubUser(); try {
const user = await github.fetchGitHubUser('zhentian-wan');
const repos = await github.fetchGitHubRepos('zhentian-wan');
console.log(user);
console.log(repos);
} catch(err) {
console.error(err);
} })();

In the example, we are doing two await operations sequentially:

        const user = await github.fetchGitHubUser('zhentian-wan');
const repos = await github.fetchGitHubRepos('zhentian-wan');

Remember that 'await' will pause the JS execution until promise resolve or reject.

So if one api request cost 0.5s then two in sequence will cose 1s.

The way to solve the problem is by converting operations to concurrently:

        const userPromise = github.fetchGitHubUser('zhentian-wan');
const reposPromise = github.fetchGitHubRepos('zhentian-wan'); const user = await userPromise;
const repos = await reposPromise; console.log(user.name);
console.log(repos.length);

Or we can use Promise.all:

        const [user, repos] = await Promise.all([
github.fetchGitHubUser('zhentian-wan'),
github.fetchGitHubRepos('zhentian-wan')
]); console.log(user.name, repos.length);

Of course, tow approaches are not the same, Promise.all will throw error if one of the request throw error.

[ES7] Await multi promises sequentially or concurrently的更多相关文章

  1. 【转】6 Reasons Why JavaScript’s Async/Await Blows Promises Away (Tutorial)

    原文:https://hackernoon.com/6-reasons-why-javascripts-async-await-blows-promises-away-tutorial-c7ec105 ...

  2. es7 await/async解决异步问题

    最近做项目遇到一个问题,前端调用ie浏览器中的ocx的方法去查询数据,查询完之后ocx给一个返回值,然后js将返回值当参数传入到另外的函数中去做数据处理,但是遇到一个问题是前端需要异步去执行这个过程 ...

  3. promise async await使用

    1.Promise (名字含义:promise为承诺,表示其他手段无法改变) Promise 对象代表一个异步操作,其不受外界影响,有三种状态: Pending(进行中.未完成的) Resolved( ...

  4. 图解 Await 和 Async

    原文链接:Await and Async Explained with Diagrams and Examples 文章目录 简介 Promise 问题:组合 Promise Async 函数 Awa ...

  5. Async/Await替代Promise的6个理由

    译者按: Node.js的异步编程方式有效提高了应用性能:然而回调地狱却让人望而生畏,Promise让我们告别回调函数,写出更优雅的异步代码:在实践过程中,却发现Promise并不完美:技术进步是无止 ...

  6. 如何避免 await/async 地狱

    原文地址:How to escape async/await hell 译文出自:夜色镇歌的个人博客 async/await 把我们从回调地狱中解救了出来,但是如果滥用就会掉进 async/await ...

  7. 一次forEach 中 await 的使用

    forEach 和 await/async 的问题 最近在刷面试提的时候看见这样一道题 const list = [1, 2, 3] const square = num => { return ...

  8. jdk8中java.util.concurrent包分析

    并发框架分类 1. Executor相关类 Interfaces. Executor is a simple standardized interface for defining custom th ...

  9. ECMAScript 6 Features 中文版

    ECMAScript 6 Features 中文版 如词不达意,欢迎提 PR & issue 采用中英混排的方式进行译制,如不解请查看对应原文 本文档将与原作者的 文档 保持同步更新,欢迎关注 ...

随机推荐

  1. hdoj 2122 Ice_cream’s world III【最小生成树】

    Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. 如果笔记本的 WIN7 运行很卡,请尝试运行这些批处理

    如果笔记本的 WIN7 运行很卡,请尝试运行这些批处理 WIN7是不是很卡?关掉下列服务吧 @echo off rem AppXSvc 为部署应用商店应用程序提供基础结构支持 rem BITS 微软的 ...

  3. MYSQL存储过程中 使用变量 做表名--转

    原文地址:http://blog.csdn.net/business122/article/details/7528859 今天写一个对数据库做快照的存储过程,用到了动态表名,突然发现MYSQL不支持 ...

  4. Fedora 13 Alpha测试手记横空出世

    本文转载在:http://www.linux521.com/2009/system/201004/10719.html   本文是继<Fedora 11-Alpha试用手记>文章(http ...

  5. 很好的资源 for android

    //texttospeach http://examples.javacodegeeks.com/android/core/text-to-speech/android-text-to-speech- ...

  6. 【Codeforces Round #455 (Div. 2) A】Generate Login

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举两个串的前缀长度就好. 组出来. 排序. 取字典序最小的那个. [代码] #include <bits/stdc++.h& ...

  7. POJ1308——Is It A Tree?

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22631   Accepted: 7756 De ...

  8. C8815 用 USB网卡(Asix AX88772 )上网

    C8815 用 USB网卡(Asix AX88772 )上网 C8815不支持给USB外设供电,不过可以使用自供电的OTG线带动USB设备 C8815最新固件中没有Asix AX88772驱动,需要自 ...

  9. 【ASP.NET开发】.NET三层架构简单解析

    对于三层架构来说,主要是使用设计模式的思想,对于项目的各个模块实现"高内聚,低耦合"的思想.这里就不做详细的介绍了,如果大家有兴趣,可以阅读软件工程和设计模式相关文章. 对于三层架 ...

  10. select下拉列表选中后,跳转新链接

    1.在当前页面打开新链接 <select onchange="location.href=this.options[this.selectedIndex].value" na ...