ECMAScript2017之async function
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的更多相关文章
- [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 ...
- [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', ...
- Node.js module export async function
一.Demo 1.首先定义 module 文件:bbb.js const fs = require("fs"); function readFileSync() { let res ...
- 关于async function(){ let res = await } 详解
本文引自: https://www.jianshu.com/p/435a8b8cc7d3 async function fn(){ //表示异步,这个函数里面有异步任务 let result = aw ...
- ES Next & Arrow function & Promise & Iterator & Generator yield & Async Await
ES Next & Arrow function & Promise & Iterator & Generator yield & Async Await co ...
- javascript异步编程的前世今生,从onclick到await/async
javascript与异步编程 为了避免资源管理等复杂性的问题, javascript被设计为单线程的语言,即使有了html5 worker,也不能直接访问dom. javascript 设计之初是为 ...
- 【TypeScript】如何在TypeScript中使用async/await,让你的代码更像C#。
[TypeScript]如何在TypeScript中使用async/await,让你的代码更像C#. async/await 提到这个东西,大家应该都很熟悉.最出名的可能就是C#中的,但也有其它语言也 ...
- async 更优雅异步体验
上一篇<让 Generator 自启动>介绍了通过起动器让 Generator 跑起来,而本篇采用 async 实现更优雅的异步编程. 从例子开始 借用上一篇例子中的例子说起. funct ...
- async 函数学习笔记
async函数就是Generator函数的语法糖. var fs = require('fs'); var readFile = function (fileName) { return new Pr ...
随机推荐
- linux 组管理
修改文件所有者 chown 用户名 文件名 修改文件所在的组 chgrp 组名 文件名 r = 4 , w = 2, x = 2 u :所有者 g :所在组 o:其他组 a: ...
- electron安装到第一个实例
1.node.js下载,然后安装.下载地址:链接:http://pan.baidu.com/s/1o7TONhS 密码:fosa 2.cmd下输入:npm install electron-prebu ...
- ISPF常用命令
[ISPF功能键] PF1: HELP帮助键 PF2: SPLIT键,改变分屏位置 PF3: END键,结束并退回上级菜单 PF4: RETURN键,结束并退回主菜单 PF5: REFIND键,重复最 ...
- unity WegGL 调用js
test.jslib文件,必须放到Assets/Plugins下,这里是:Assets/Plugins/WebGL mergeInto(LibraryManager.library, { Hello: ...
- Servlet开发
源地址:http://www.cnblogs.com/xdp-gacl/p/3760336.html 一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun ...
- suse 关于使用 /etc/init.d/boot.local的问题
最近看了一个问题,有同事在 suse环境下的/etc/init.d/boot.local 中,增加了一行脚本. 该脚本的简单大意如下: #!/bin/bash ] do ] then echo &qu ...
- How to Pronounce the 50 States
How to Pronounce the 50 States (1/4) Share Tweet Share Tagged With: Places The US state names can be ...
- maven ,添加加密算法,使用
1:消息摘要:(数字指纹):既对一个任意长度的一个数据块进行计算,产生一个唯一指纹.MD5/SHA1发送给其他人你的信息和摘要,其他人用相同的加密方法得到摘要,最后进行比较摘要是否相同. MD5(Me ...
- 那些年,我们追过的PHP自加自减运算(2)
----------------------------------------------------------------------------------------- 先来一段例子,来引出 ...
- 20.pipe
pipe相当于angular1里面的filter 做一些格式转换啊,或者从一个数组里面选取一个元素等等 只要你愿意可以定义很复杂的内容‘’ 我们先看看 angular2 里面自带的一些pipe 我们去 ...