webveuje/es6/async.html
2021-03-23 10:58:10 +08:00

78 lines
2.2 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
// const fs = require('fs');
// const readFile = function (fileName) {
// return new Promise(function (resolve, reject) {
// fs.readFile(fileName, function (error, data) {
// if (error) return reject(error);
// resolve(data);
// });
// });
// };
// const gen = function* () {
// const f1 = yield readFile('/etc/fstab');
// const f2 = yield readFile('/etc/shells');
// console.log(f1.toString());
// console.log(f2.toString());
// };
// // *是 Generator 的声明
// const gen2 = async function () {
// const f1 = await readFile("file1")
// const f2 = await readFile("file2")
// console.log(f1.toString())
// console.log(f2.toString())
// }
// // async 声明这个函数是 异步函数 只有在被async修饰的函数中才能使用await 关键字
// // await 等待后面的操作完成后才继续执行
// async function getStockPriceByName(name) {
// const symbol = await getStockSymbol(name);
// const stockPrice = await getStockPrice(symbol);
// return stockPrice;
// }
// getStockPriceByName('goog').then(function (result) {
// console.log(result);
// });
async function testAsync() {
return "hello async";
}
async function t1(){
var data = testAsync();
console.log(data)
}
async function t2(){
var data = await testAsync();
console.log(data)
}
t1() //没加await修饰的时候 返回的是promise对象
t2() // 加了await的时候 返回的是promise对象中的value的值 即 hello async!
// var list =await getlist("url");
// var uinfo =await getinfo("url");
// 。。。
</script>
</head>
<body>
</body>
</html>