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. day41-解决粘包问题

    一.socket缓冲区 研究粘包之前先看看socket缓冲区的问题: 二.socket缓存区的详细解释 每个socket被创建后,都会分配两个缓冲区,输入缓冲区和输出缓冲区. write()/send ...

  2. 小麦成长记-<专业盗图好几年>

    ========================================图片来源朋友圈的朋友~

  3. python中pop()函数的用法

    pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值. 语法:list.pop(obj=list[-1]) //默认为 index=-1,删除最后一个列表值. obj -- ...

  4. iOS设计标注处理方法

    如果设计只给3x的设计图 在做2x适配时有几种处理方法: 按逻辑像素,大小不变,比如3x手机上一张图的逻辑像素设为24x24point,那么2x手机上这张图的大小也设为24x24point,一般适用于 ...

  5. UnicodeDecodeError: 'ascii' codec can't decode byte 0x9c in position 1: ordinal not in range(128)

    待研究: compressed_data = zlib.compress(json.dumps(data), 9) file_data = MySQLdb.escape_string(compress ...

  6. vue:再vue-cli项目中使用window以及调用window上的方法

    一: 1:在main.js中 Vue.prototype.myfunction = function() {/*你的自定义Vue方法*/} 2:在mounted(或其他生命周期中) 或者 method ...

  7. VirtualAlloc申请进程空间

    https://baike.baidu.com/item/VirtualAlloc       百度百科 https://msdn.microsoft.com/zh-cn/library/window ...

  8. LeetCode OJ 49. Group Anagrams

    题目 Given an array of strings, group anagrams together. For example, given: ["eat", "t ...

  9. oracle函数大全-字符处理函

    字符函数——返回字符值 这些函数全都接收的是字符族类型的参数(CHR 除外)并且返回字符值.除了特别说明的之外,这些函数大部分返回VARCHAR2类型的数值.字符函数的返回类型所受的限制和基本数据库类 ...

  10. 关于xml中自动提示功能的设置

    我们在编写xml文件时如果有自动提示功能,将会事半功倍,下面我就怎么设置xml进行说明: 在xml文件的开始几行一般有编写xml文件的语法要求;如 <!DOCTYPE hibernate-con ...