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. vue父子组件嵌套的时候遇到 - Component template should contain exactly one root element. If you are using v-i

    转自:https://blog.csdn.net/yangyiboshigou/article/details/72084619

  2. Spring Cloud (3)B Ribbon 负载均衡 IRule

    package com.service.config; import com.netflix.loadbalancer.IRule;import com.netflix.loadbalancer.Ra ...

  3. OWASP安全编码规范快速参考指南

    0x00 原则 概览 开发安全的软件需要对安全原则有基本的了解.虽然对于安全原则的全面评估超出了本指南的范围,但是我们还是提供了一个快速的概览.软件安全的目标是要维护信息资源的 保密性 ,  完整性 ...

  4. JEECG-P3开发专题 - 开发环境搭建入门

    官方标准开发工具: 1 .IDE Eclipse Java EE IDE for Web Developers. Version: Mars.2 Release (4.5.2) Build id: 2 ...

  5. 尚硅谷springboot学习1-简介

    以前看过springboot的相关知识,当时偷懒没有做笔记,现在忘得已经差不多了,现在趁着过年有时间,再学习一遍,并做下笔记以备忘. 特性 Spring Boot来简化Spring应用开发,约定大于配 ...

  6. Delphi的程序单元、结构、基础知识(转)

    Object Passal的程序结构很特殊,与其它语言如C++,Object Windows等结构都不同.一个Delphi程序由多个称为单元的源代码模块组成.使用单元可以把一个大型程序分成多个逻辑相关 ...

  7. 在Linux中简单实现回收子进程

    学习到wait函数了,这个函数的作用是用来回收进程.一般来说,正常退出的进程是不需要我们来专门回收的.但是进程有这两种:孤儿进程和僵尸进程. 孤儿进程: 通俗点说就是父进程先于子进程死亡.此时子进程就 ...

  8. Java API下载和查阅方法

    使用来自API的类是简单的.只要把它当做自己写的就可以,采用import来引用,可以节省自己编程的气力~ 1.API文档下载地址 https://www.oracle.com/technetwork/ ...

  9. 解决mysql从windows迁移到centos出现乱码问题

    windows上的数据库编码情况 修改前centos上的编码情况 修改centos上mysql的配置文件my.cnf如下(修改前停掉mysql服务,/etc/init.d/mysqld stop) 红 ...

  10. 360Top奢侈品演示站 - 纯手工纪念品

    一个纯手写的过程化编程商城项目,留作纪念. 360Top奢侈品演示站 http://360top.farwish.com