1、回调地狱
缺点:
1. 代码的耦合度太高
2. 大量冗余的代码相互嵌套,代码的可读性变差
2、Promise的基本概念
promise是一个构造函数:
1. 我们可以创建Promise的实例 p,const p = new Promise();
2. new 出来的实例对象代表一个异步操作
promise.prototype上包含一个.then()方法:
1. 每一次new Promise()构造函数得到实例对象
2. 都可以通过原型链的方式访问到.then()方法, p.then()
.then()方法预先指定成功和失败的回调函数
1. p.then(成功的回调函数,失败的回调函数)
2. p.then(result => { }, error => { })
3. 调用 .then() 方法时,成功的回调函数是必选的、失败的回调函数是可选的
3、then-fs的基本使用
调用 then-fs 提供的 readFile() 方法,可以异步地读取文件的内容,它的返回值是 Promise 的实例对象。因
此可以调用 .then() 方法为每个 Promise 异步操作指定成功和失败之后的回调函数
/**
* 基于Promise的方式读取文件
*/
import thenFs from 'then-fs'
thenFs.readFile('./1.txt', 'utf8').then((r1) => { console.log(r1) })
thenFs.readFile('./2.txt', 'utf8').then((r2) => { console.log(r2) })
thenFs.readFile('./3.txt', 'utf8').then((r3) => { console.log(r3) })
上述代码无法保证文件的顺序读取,需要进一步改进
/**
* 基于Promise的方式读取文件
*/
import thenFs from 'then-fs'
thenFs.readFile('./11.txt', 'utf8').then((r1) => {
console.log(r1);
return thenFs.readFile('./2.txt', 'utf8');
}).then((r2) => {
console.log(r2);
return thenFs.readFile('./3.txt', 'utf8');
}).then((r3) => {
console.log(r3);
})
4、通过.catch捕获错误
/**
* 基于Promise的方式读取文件
*/
import thenFs from 'then-fs'
thenFs.readFile('./1.txt', 'utf8').then((r1) => {
console.log(r1);
return thenFs.readFile('./2.txt', 'utf8');
}).then((r2) => {
console.log(r2);
return thenFs.readFile('./3.txt', 'utf8');
}).then((r3) => {
console.log(r3);
}).catch((err) => {
console.log(err);
})
如果不希望前面的错误导致后面的.then无法正常执行,则可将.catch的调用提前
/**
* 基于Promise的方式读取文件
*/
import thenFs from 'then-fs'
thenFs.readFile('./1.txt', 'utf8')
.catch((err) => {
console.log(err);
}).then((r1) => {
console.log(r1);
return thenFs.readFile('./2.txt', 'utf8');
}).then((r2) => {
console.log(r2);
return thenFs.readFile('./3.txt', 'utf8');
}).then((r3) => {
console.log(r3);
})
5、Promise.all()方法
会发起并行的Promise异步操作,等所有的异步操作执行完毕之后才会执行.then操作(等待机制)
import thenFs from 'then-fs'
const promiseArr = [
thenFs.readFile('./1.txt', 'utf8'),
thenFs.readFile('./2.txt', 'utf8'),
thenFs.readFile('./3.txt', 'utf8'),
]
Promise.all(promiseArr)
.then(([r1, r2, r3]) => { //所有文件读取成功(等待机制)
console.log(r1, r2, r3);
}).catch(err => { //捕获promise异步操作中的错误
console.log(err);
})
6、Promise.race()方法
Promise.race() 方法会发起并行的 Promise 异步操作,只要任何一个异步操作完成,就立即执行下一步的
.then 操作(赛跑机制)
import thenFs from 'then-fs'
const promiseArr = [
thenFs.readFile('./1.txt', 'utf8'),
thenFs.readFile('./2.txt', 'utf8'),
thenFs.readFile('./3.txt', 'utf8'),
]
Promise.all(promiseArr)
.then(result => { //所有文件读取成功(等待机制)
console.log(result);
}).catch(err => { //捕获promise异步操作中的错误
console.log(err.message);
})
7、基于Promise封装读文件的方法
方法的封装要求:
① 方法的名称要定义为 getFile
② 方法接收一个形参 fpath,表示要读取的文件的路径
③ 方法的返回值为 Promise 实例对象
import fs from 'fs'
function getFile(path) {
return new Promise(function () {
fs.readFile(path, 'utf8', (err, suc) => {})
})
}
8、调用 resolve 和 reject 回调函数
import fs from 'fs'
function getFile(path) {
return new Promise(function (resolve, reject) {
fs.readFile(path, 'utf8', (err, suc) => {
if (err) {
return reject(err);
} else {
return resolve(suc);
}
})
})
}
getFile('./11.txt').then(
(suc) => {
console.log(suc);
},
(err) => {
console.log(err.message);
})