ES7
Array.prototype.inclueds
const arr = [1, 2, 3];
console.log(arr.inclueds(1)); // true
幂运算
let a = 2 ** 3;
console.log(a); // 8
ES8
async
async函数是异步编程的一种
- async函数返回 Promise 对象
- Promise 对象的结果由 async 函数执行的返回值决定
- async函数返回 Promise 对象后可以使用 then() 处理结果
async function fn() {
console.log('hello');
}
const result = fn();
console.log(result); // Promise {<fulfilled>: undefined}
await
await 需要结合 async函数
- await 必须写在 async 函数中
- await 右侧的表达式一般为 Promise
- await 成功时返回 Promise 的值
- await 失败时抛出异常,需要使用 try...catch... 捕获
// 成功的
const p = new Promise((resolve, reject) => {
resolve('success');
});
async function fn() {
let result = await p;
console.log(result);
}
fn();
// 失败的
const p = new Promise((resolve, reject) => {
reject('failure');
});
async function fn() {
try {
let result = await p;
console.log(result);
} catch (e) {
console.log(e);
}
}
fn();
ES11
动态import
动态 import 模块使用 import() 方法,其返回一个 Promise 对象。
function imp() {
import('./app.js').then((module)=>{
console.log(module);
});
}