An async function can contain an await expression that pauses the execution of the async function and waits for the passed Promise's resolution, and then resumes the async function's execution and returns the resolved value.

Remember, the await keyword is only valid inside async functions. If you use it outside of an async function's body, you will get a SyntaxError.

function getHtml(url) {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = () => {
resolve(xhr.responseText);
};
xhr.onerror = () => {
reject(xhr.statusText)
};
xhr.send();
}).then(
val=>{
return getTitle(val);
}
);
}
function getTitle(html){
return html.substring(html.indexOf('<title>')+7,html.indexOf('</title>'));
}
async function printTitle(){
console.log(new Date().getTime());
//  暂停执行,直到Promise被resolve或reject后,继续执行
var title = await getHtml('https://www.baidu.com');
console.log(new Date().getTime(),title);
}
printTitle();

ECMAScript2017之async function的更多相关文章

  1. [Javascript] Await a JavaScript Promise in an async Function with the await Operator

    The await operator is used to wait for a promise to settle. It pauses the execution of an async func ...

  2. [Unit Testing] Test async function with Jasmine

    Most of time, when we want to test function call inside a promise, we can do: it('Should be async', ...

  3. Node.js module export async function

    一.Demo 1.首先定义 module 文件:bbb.js const fs = require("fs"); function readFileSync() { let res ...

  4. 关于async function(){ let res = await } 详解

    本文引自: https://www.jianshu.com/p/435a8b8cc7d3 async function fn(){ //表示异步,这个函数里面有异步任务 let result = aw ...

  5. ES Next & Arrow function & Promise & Iterator & Generator yield & Async Await

    ES Next & Arrow function & Promise & Iterator & Generator yield & Async Await co ...

  6. javascript异步编程的前世今生,从onclick到await/async

    javascript与异步编程 为了避免资源管理等复杂性的问题, javascript被设计为单线程的语言,即使有了html5 worker,也不能直接访问dom. javascript 设计之初是为 ...

  7. 【TypeScript】如何在TypeScript中使用async/await,让你的代码更像C#。

    [TypeScript]如何在TypeScript中使用async/await,让你的代码更像C#. async/await 提到这个东西,大家应该都很熟悉.最出名的可能就是C#中的,但也有其它语言也 ...

  8. async 更优雅异步体验

    上一篇<让 Generator 自启动>介绍了通过起动器让 Generator 跑起来,而本篇采用 async 实现更优雅的异步编程. 从例子开始 借用上一篇例子中的例子说起. funct ...

  9. async 函数学习笔记

    async函数就是Generator函数的语法糖. var fs = require('fs'); var readFile = function (fileName) { return new Pr ...

随机推荐

  1. cordova- cordova-plugin-splashscreen启动页面和图标的设置

    https://www.cnblogs.com/a418120186/p/5856371.html

  2. SITE STRUCTURE

    SITE STRUCTURE HTML Review Congratulations! You've learned enough HTML to create a great website! Be ...

  3. docker-compose初试及命令基础

    转自:https://www.cnblogs.com/jsonhc/p/7811929.html 以一个简单的lnmp.yaml的配置文件进行讲解docker-compose命令的基础讲解,熟练掌握命 ...

  4. UE 不生成.bak文件

    .bak文件是UE处理文件时自动备份的文件,可以取消备份这样就不会生成.bak文件了 菜单:高级-设置-文件处理-备份        应用和确定

  5. linux内核中的const成员是否可以修改?

    本文的基础知识:由于前半部分内容是转的,且不知道原文出处,没法给出原文地址,大家自行百度 const的实现机制 const究竟是如何实现的呢?对于声明为const的内置类型,例如int,short,l ...

  6. 关于T-SQL中exists或者not exists子查询的“伪优化”的做法

    问题起源 在使用t-sql中的exists(或者not exists)子查询的时候,不知道什么时候开始,发现一小部分人存在一种“伪优化”的一些做法,并且向不明真相的群众传递这一种写法“优越性”,实在看 ...

  7. 解决运行wamp提示“MSVCR110.dll”丢失的问题!

    我在Windows系统上安装wampserver2.5 64位,安装到最后,总是提示丢失msvcr110.dll 解决办法: 到这个网站下载一个Visual C++ Redistributable f ...

  8. winform中datagridview刷新后的排序记忆

    datagridview先点标题排序,但是重新刷新之后,还是变成窗体加载后的样子 我这里用定时器刷新的. 1.先定义三个全局变量 /// <summary> /// 需要排序的列和方向 / ...

  9. C++ 关于 CMFCPropertyGridCtrl 的使用方法 之二 (原创)

    接上一节所讲,这一节咱们重点讲一下CMFCPropertyGridCtrl 所支持的数据表格的建立过程 在上一节中,咱们已经了解到了 CMFCPropertyGridCtrl  是要用到实例函数:Ad ...

  10. effective C++学习二(仅供个人学习记录,本文摘录effective C++)

    条款 2:尽量用<iostream>而不用<stdio.h> scanf 和 printf 很轻巧,很高效,你也早就知道怎么用它们,这我承 认.但尽管他们很有用,事实上 sca ...