<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ES7的async/await</title>
<style type="text/css">
</style>
</head>
<body>
<script type="text/javascript">
// 模拟其他语言中的 sleep,实际上可以是任何异步操作
const sleep = (timeountMS) => new Promise((resolve) => {
setTimeout(resolve, timeountMS);
});
(async () => { // 声明即执行的 async 函数表达式
for (var i = 0; i < 5; i++) {
await sleep(1000);
console.log(new Date, i);
}
await sleep(1000);
console.log(new Date, i);
})();
var start = async function () {
// 在这里使用起来就像同步代码那样直观
console.log('start');
await sleep(3000);
console.log('end');
};
start();
</script>
</body>
</html>