async/await 继发执行与并发执行,看如何控制

两个异步函数 foo bar

function foo() {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('foo:' + new Date().toLocaleString())
resolve('foo')
}, 2000)
})
} function bar() {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('bar:' + new Date().toLocaleString())
resolve('bar')
}, 2000)
})
}

继发执行

//继发执行
async function main() {
console.log('beginTime:' + new Date().toLocaleString())
var foostr = await foo();
console.log(new Date().toLocaleString())
console.log(foostr);
var barstr = await bar();
console.log(new Date().toLocaleString())
console.log(barstr);
console.log('endTime:' + new Date().toLocaleString())
} //继发执行
async function main2() {
let docs = [foo, bar];
console.log('beginTime:' + new Date().toLocaleString())
for (let doc of docs) {
var str = await doc();
console.log(new Date().toLocaleString())
console.log(str);
}
console.log('endTime:' + new Date().toLocaleString())
}

并发执行

//并发执行
async function async_main() {
console.log('beginTime:' + new Date().toLocaleString())
let [get_foo, get_bar] = await Promise.all([foo(), bar()]);
console.log(new Date().toLocaleString());
console.log(get_foo);
console.log(get_bar);
console.log('endTime:' + new Date().toLocaleString())
} //并发执行
async function async_main4() {
let docs = [foo(), bar()];
console.log('beginTime:' + new Date().toLocaleString())
for (let doc of docs) {
var str = await doc;
console.log(new Date().toLocaleString())
console.log(str);
}
console.log('endTime:' + new Date().toLocaleString()) } //并发执行
async function async_main2() {
console.log('beginTime:' + new Date().toLocaleString())
let fooPromise = foo();
let barPromise = bar();
let get_foo = await fooPromise;
console.log(new Date().toLocaleString());
console.log(get_foo);
let get_bar = await barPromise;
console.log(new Date().toLocaleString());
console.log(get_bar);
console.log('endTime:' + new Date().toLocaleString())
} //并发执行 但是 会先执行 endTime
async function async_main3() {
let docs = [foo, bar];
console.log('beginTime:' + new Date().toLocaleString())
docs.forEach(async (val) => {
var str = await val();
console.log(new Date().toLocaleString())
console.log(str);
})
console.log('endTime:' + new Date().toLocaleString())
}

node.js async/await 继发执行与并发执行的更多相关文章

  1. [node.js] async/await如何优雅处理异常?

    node.js的世界,从callback开始,不会止于async. 所有人都在骂为什么不能完全进化,其实我感觉这就是老外的细心,为了承上.这也就是为什么async其实就是promise一样,假如不是一 ...

  2. 关于async/await、promise和setTimeout执行顺序

    先来一道关于async/await.promise和setTimeout的执行顺序的题目: async function async1() { console.log('async1 start'); ...

  3. js async await 终极异步解决方案

    既然有了promise 为什么还要有async await ? 当然是promise 也不是完美的异步解决方案,而 async await 的写法看起来更加简单且容易理解. 回顾 Promise Pr ...

  4. js async/await

    一.async 带async关键字的函数,是声明异步函数,返回值是promise对象,如果async关键字函数返回的不是promise,会自动用Promise.resolve()包装. async f ...

  5. [转] js async await 终极异步解决方案

    阅读目录 回顾 Promise async await 字面理解 async.await 如何执行 await 操作符 总结 既然有了promise 为什么还要有async await ? 当然是pr ...

  6. Node.js——Async

    一:流程控制 为了适应异步编程,减少回调的嵌套,我尝试了很多库.最终觉得还是async最靠谱. 地址:https://github.com/caolan/async Async的内容分为三部分: 流程 ...

  7. 【原创】cs+html+js+css模式(七): 顺序执行与并发执行问题,IIS7及其以上版本的抛错问题解决

          在进行开发的过程中,针对于这种模式,我们继承的IRequiresSessionState,这种对于我们的同一个IIS的执行中是顺序执行即一个ajax请求处理完成后,才能执行下一个ajax, ...

  8. node.js async 几个函数

    async.waterfallasync.seriesasync.parallelasync.auto http://my.oschina.net/huangsz/blog/176203http:// ...

  9. node.js:怎样同时执行多条SQLs,且只有一个回调

    本文主要介绍开源node.js库mysql-queries,其可以同时执行多条SQLs,且只有一个回调.同时抛砖引玉,与大家交流node.js开发经验. node.js很大的特点就是事件驱动.非阻塞和 ...

随机推荐

  1. Visual odometry and zed's IMU fusion on RTAB-Map

    "When using /camera/odom, you don't need to use visual_odometry node. rtabmap should be subscri ...

  2. linux下vim python代码自动补全

    一.vim python自动补全插件:pydiction 可以实现下面python代码的自动补全: 1.简单python关键词补全 2.python 函数补全带括号 3.python 模块补全 4.p ...

  3. 跟我一起读postgresql源码(二)——Parser(查询分析模块)

    上篇博客简要的介绍了下psql命令行客户端的前台代码.这一次,我们来看看后台的代码吧. 十分不好意思的是,上篇博客我们只说明了前台登陆的代码,没有介绍前台登陆过程中,后台是如何工作的.即:后台接到前台 ...

  4. CF914E Palindromes in a Tree

    $ \color{#0066ff}{ 题目描述 }$ 给你一颗 n 个顶点的树(连通无环图).顶点从 1 到 n 编号,并且每个顶点对应一个在'a'到't'的字母. 树上的一条路径是回文是指至少有一个 ...

  5. SDUT OJ 顺序表应用3:元素位置互换之移位算法

    顺序表应用3:元素位置互换之移位算法 Time Limit: 1000 ms Memory Limit: 570 KiB Submit Statistic Discuss Problem Descri ...

  6. leetcode-229.求众数(二)

     题目: 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为O(n),空间复杂度为O(1). 示例 1: 输入: [3,2,3] 输出: [3 ...

  7. .net mvc 框架实现后台管理系统4-layedit使用

    做个简单的文章发布,使用了layui的layedit 效果: 在html页面添加: <textarea id="MyArticleContent" style="d ...

  8. winform工具1-图片去除水印

    效果图: 思路: 1.获取图片 2.处理水印 3.保存处理的图片 代码: 获取图片: private void button1_Click(object sender, EventArgs e) { ...

  9. 利用EFCore 封装Repository(可扩展不同数据的sql操作)

    本篇是对EFCore 进行下封装并实现基本的增删改查的同步异步方法及针对不同数据库的批量插入.sql语句直接操作数据库: 一. 先定义基础仓储接口IRepository public interfac ...

  10. 什么是redis?Reids的特点是什么?Redis支持的数据类型有哪些?

    首先,分布式缓存框架 可以 看成是nosql的一种 (1)什么是redis? redis 是一个基于内存的高性能key-value数据库. (有空再补充,有理解错误或不足欢迎指正) (2)Reids的 ...