先用一个例子来说明async await promise的执行顺序

console.log('start');

async function test(){
console.log('111'); await new Promise((resolve,reject)=>{
setTimeout(()=>{
console.log('finish')
resolve('haha');
},6000)
}) console.log('start promise2') return await new Promise((resolve,reject)=>{
setTimeout(()=>{
console.log('finish2')
resolve('haha2');
},6000)
})
} test().then((data)=>
console.log(data)
) console.log('end');

该段代码的执行结果为:

上面的代码很好的显示了执行顺序,其实async 就是 generator 函数的语法糖,即async -->  function* (){}

await 是generator函数里面的yield的语法糖。

同时,执行generator 函数时,generator函数并没有立即执行,只是返回了一个可遍历的对象,当执行next()方法时才开始执行函数内的代码,直到 yield 或者return停止,并返回yield后面的值。

async略微有点区别:

调用 async 的函数时,函数里面的代码会立即执行,直到await处停止执行内部代码,同时跳出该方法,执行async 函数外面的后续代码,当await处的异步方法返回结果后,再回来执行,所以就有了上面的执行顺序。

Note:如果await前没有return的话,then回调里面将返回undefined

async await promise 执行时序的更多相关文章

  1. 理解koa2 之 async + await + promise

    koa是下一代的Node.js web框架. 我们首先使用koa来实现一个简单的hello world吧!假如目前的项目结构如下: ### 目录结构如下: koa-demo1 # 工程名 | |--- ...

  2. async await 的执行

    async await的执行 注意:本次代码仅在 Chrome 73 下进行测试. start 不了解 async await 的,先去看阮一峰老师的文章async 函数. 先来看一道头条的面试题,这 ...

  3. 理解 async/await 的执行

    这是一篇简单的短文章,方便理解. 开局先丢官宣:sec-async-function-definitions 这个链接是对 await 的解释,解释了它的执行. await 的执行意味着(官宣巴拉巴拉 ...

  4. node.js async/await 继发执行与并发执行

    async/await 继发执行与并发执行,看如何控制 两个异步函数 foo bar function foo() { return new Promise((resolve, reject) =&g ...

  5. async await promise

    async 异步函数,以后可能会用得很广. 1.箭头函数: 没有{ }时不写return 也有返回值 2.Promise : 异步神器,很多异步api都是基于Promise 3.new Promise ...

  6. JS中的async/await的执行顺序详解

    虽然大家知道async/await,但是很多人对这个方法中内部怎么执行的还不是很了解,本文是我看了一遍技术博客理解 JavaScript 的 async/await(如果对async/await不熟悉 ...

  7. setTimeout、Promise、Async/Await 的执行顺序

    问题描述:以下这段代码的执行结果 async function async1() { console.log('async1 start'); await async2(); console.log( ...

  8. Async/await promise实现

    An async function can contain an await expression that pauses the execution of the async function an ...

  9. .net 关于Task.Run 和 Async await的执行顺序

    一直捋不清楚用Task.Run异步的执行关系,网上找的些说明写得也有点复杂,所以自己做实验测一下. 直接上代码 这个是加await private static void TestFun() { Co ...

随机推荐

  1. 公告:《那些年,追寻Jmeter的足迹》上线

    在我们团队的努力下,我们<那些年,追寻Jmeter的足迹>手册第1版本工作完成(后面还会有第2版本),比较偏基础,这是汇集我们团队的经验和团队需要用到的知识点来整理的,在第2个版本,我们整 ...

  2. Tomcat必会的企业级配置调优

    Tomcat服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选. ======== 完美的分割线 ...

  3. C语言SQLite3基本操作Demo

    /************************************************************************** * C语言SQLite3基本操作Demo * 声 ...

  4. 【01_292】Nim Game

    Nim Game Total Accepted: 25342 Total Submissions: 50672 Difficulty: Easy You are playing the followi ...

  5. 51Nod:活动安排问题(区间问题)

    X轴上有N条线段,每条线段有1个起点S和终点E.最多能够选出多少条互不重叠的线段.(注:起点或终点重叠,不算重叠). 例如:[1 5][2 3][3 6],可以选[2 3][3 6],这2条线段互不重 ...

  6. sublime text 3 实用的快捷键

    Ctrl+Shift+P:打开命令面板Ctrl+P:搜索项目中的文件Ctrl+G:跳转到第几行Ctrl+W:关闭当前打开文件Ctrl+Shift+W:关闭所有打开文件Ctrl+Shift+V:粘贴并格 ...

  7. Cannot find config.m4. Make sure that you run '/usr/local/php/bin/phpize' in the top level source directory of the module的 解决方法

    cp /php-7.1.22/ext/openssl/config0.m4 /usr/local/php/bin/config.m4

  8. LeetCode Factorial Trailing Zeroes Python

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...

  9. spket插件的安装与使用完整图文版

    下载最新破解版的spket1.6.18(见下面附件) 对于目前的MyEclipse的插件安装是很简单的,把spket1.6.18破解版.zip解压后直接复制到MyEclipse安装目录的dropins ...

  10. node express 返回json object

    web 开发的过程中我们经常需要返回对象的json 格式,使用node express 是比较简单的, 1.node express 基础网站的创建 比较简单,以前的文章有 2.编写对象并导出对象 / ...