1.async 函数
async 函数的返回值类型:
- Promise 类型(大多数情况)
- Error 类型
- 其他非 Promise 类型
无论 async 函数 return 什么类型的值,async 函数的返回值均为 Promise 对象
(1) Promise 类型
若返回的 Promise 对象处理成功,则函数的返回值为 成功的(fulfilled) Promise 对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| async function fn() { return new Promise((resolve, reject) => { resolve("process success"); }); }
const result = fn(); console.log(result);
result.then(value => { console.log(value); }, reason => { console.warn(reason); });
|
若返回的 Promise 对象处理失败,则函数的返回值为 失败的(rejected) Promise 对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| async function fn() { return new Promise((resolve, reject) => { reject("process failed"); }); }
const result = fn(); console.log(result);
result.then(value => { console.log(value); }, reason => { console.warn(reason); });
|
(2) Error 类型
若 async 函数发生 Error,则返回失败的(rejected) Promise 对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| async function fn() { throw new Error("Server Error"); }
const result = fn(); console.log(result);
result.then(value => { console.log(value); }, reason => { console.warn(reason); });
|
(3) 其他非 Promise 类型
若 async 函数返回其他非 Promise 类型,则 async 函数的返回值总是一个成功的(fulfilled) Promise 对象
- return string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| async function fn() { return "abc"; }
const result = fn(); console.log(result);
result.then(value => { console.log(value); }, reason => { console.warn(reason); });
|
- return boolean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| async function fn() { return false; }
const result = fn(); console.log(result);
result.then(value => { console.log(value); }, reason => { console.warn(reason); });
|
- return;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| async function fn() { return; }
const result = fn(); console.log(result);
result.then(value => { console.log(value); }, reason => { console.warn(reason); });
|
- 没有 return
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| async function fn() {
}
const result = fn(); console.log(result);
result.then(value => { console.log(value); }, reason => { console.warn(reason); });
|
2.await 表达式
- await 必须写在 async 函数中
- await 右侧的表达式一般为 promise 对象
- await 返回的是 promise 成功的值
- await 的 promise 失败了, 就会抛出异常, 需要通过 try…catch 捕获处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| const p = new Promise((resolve, reject) => { resolve("info data"); })
async function main() { try { let result = await p; console.log(result); } catch (error) { console.log(error); } }
main();
|
3.async 和 await 读取文件内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| const fs = require("fs");
function readFile1() { return new Promise((resolve, reject) => { fs.readFile("./resources/file1.txt", (err, data) => { if (err) { } else { resolve(data); } }) }) }
function readFile2() { return new Promise((resolve, reject) => { fs.readFile("./resources/file2.txt", (err, data) => { if (err) { } else { resolve(data); } }) }) }
function readFile3() { return new Promise((resolve, reject) => { fs.readFile("./resources/file3.txt", (err, data) => { if (err) { } else { resolve(data); } }) }) }
async function main() { let result1 = await readFile1(); let result2 = await readFile2(); let result3 = await readFile3();
console.log(result1.toString()); console.log(result2.toString()); console.log(result3.toString()); }
main();
|
4.async 和 await 封装 AJAX 请求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| function sendAjax(url) { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.send();
xhr.onreadystatechange = function() { if (xhr.readyState == 4) { if (xhr.status >= 200 && xhr.status < 300) { resolve(xhr.response); } else { reject(xhr.status); } } } }); }
sendAjax("https://api.apiopen.top/api/sentences").then(value => { console.log(value); }, reason => {
})
async function main() { let result = await sendAjax("https://api.apiopen.top/api/sentences"); let weather = await sendAjax("https://www.tianqiapi.com/api/?version=v1&city=%E5%8C%97%E4%BA%AC&appid=23941491&appsecret=TXoD5e8P");
console.log(result); console.log(weather); }
main();
|