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 ...
随机推荐
- ubuntu16.04搭建geodjango+postgresql+postgis的WebGIS框架(一)安装第三方空间库
postgis是postgresql的空间扩展对象,它需要一些第三方库的支持.包括GEOS, PROJ.4 和 GDAL.我们首先安装这几个空间库. 在ubuntu系统终端执行:(预先装一些依赖的库) ...
- Oracle表复杂查询
转自:https://www.cnblogs.com/w-gao/p/7288293.html Oracle表复杂查询 聚合函数 max(字段值) -- 求最大值 min(字段值) -- 求最小值 ...
- C++中多态中构造函数与析构函数的调用
做个实验,看一下成员变量的构造析构,父类子类的构造析构,以及虚函数对调用的影响. #include <iostream> using namespace std; class Member ...
- Mysql 定时任务事件
参考文献:https://blog.csdn.net/dream_ll/article/details/73499750
- JSdom操作内容,样式,属性
<p> JavaScript 能够直接写入 HTML 输出流中: </p> <script> document.write("<h1>This ...
- Unicode UTF8 UTF16 urlencode base64
Unicode:是一个字符集,每个字符对应一个唯一的unicode编码,一般是16位. UTF8是针对Unicode的编码方式,因为如果每个字符都用unicode的编码存储的话会很浪费空间,比如说as ...
- newCachedThreadPool使用案例
newCachedThreadPool 缓存默认60s 猜下你的结果 package com.juc.threadpool; import java.util.concurrent.ExecutorS ...
- Python3.7中urllib.urlopen 报错问题
import urllib web = urllib.urlopen('https://www.baidu.com') f = web.read() print(f) 报错: Traceback (m ...
- git使用 从远程库克隆和更新到本地
从远程库克隆到本地. git clone git@github.com:kingbook/Framework.git 或 git clone http://github.com/kingBook/Fr ...
- 去除文件BOM头工具
<?php /** * 用法:复制以下代码至新建的php文件中,将该php文件放置项目目录,运行即可.代码来源于网络. * chenwei 注. */ header('content-Type: ...