Promise

/*### 1、回调地狱
多层回调函数的相互嵌套,就形成了回调地狱。

  • 回调地狱缺点
    • 代码耦合性太强,牵一发而动全身,难以维护
    • 大量冗余的代码相互嵌套,代码的可读性差

为了解决回调地狱的问题,ES6中新增了Promise的概念

2、Promise的基本概念

  • Promise是一个构造函数
    • 可以创建Promise实例:const p = new Promise()
    • new出来的Promise实例对象,代表一个异步操作
  • Promise.prototype上包含一个.then()方法
    • 每一次new Promise()构造函数得到的实例对象,都可以通过原型链的方式访问到.then()方法。例如:p.then()
  • .then()方法可以用来预先指定成功或者失败的回调函数
    • p.then ( 成功的回调函数 , 失败的回调函数 )
    • p.then ( result => {} , error => {} )
    • 调用.then()方法时,成功的回调函数是必选的,失败的回调函数是可选的
  • then-fs的基本使用
    • 调用then-fs提供的read File()方法,可以异步地读取文件的内容,它的返回值是Promise的实例对象
    • 可以调用.then()方法为每个Promise异步操作指定成功和失败之后的回调函数
// 基于Promise的方式读取文件
import thenFs from 'then-fs'
//注意,.then()中的失败回调是可选的,可以被省略
thenFs.readFile('./files/1.txt' , 'utf8').then(r1 => { console.log(r1);} , err1 => { console.log(err1.message);} )
thenFs.readFile('./files/2.txt' , 'utf8').then(r2 => { console.log(r2);} , err2 => { console.log(err2.message);} )
thenFs.readFile('./files/3.txt' , 'utf8').then(r3 => { console.log(r3);} , err3 => { console.log(err3.message);} )

注意:上述的代码无法保证文件的读取顺序,需要进一步改进

3、.then()方法的特性

  • 如果上一个.then()方法中返回了一个新的Promise实例对象,则可以通过下一个.then()方法继续进行处理
  • 通过.then()方法的链式调用,就解决了回调地狱的问题
import thenFs from 'then-fs'
thenFs.readfile('./files/1.txt' , 'utf8').then((r1) => {
  console.log(r1);
  //第一个文件读取成功,则成功的回调函数中直接读取第二个文件
  return thenFs.readfile('*./files/2.txt' , 'utf8')
})
.then((r2) => {
  console.log(r2);
  return thenFs.readfile('./files/3.txt' , 'utf8')
})
.then((r3) => {
  console.log(r3);
})
  • 如果Promise的链式操作中如果发生了错误,可以使用Promise.prototype.catch方法进行捕获和处理
  • 在上述代码中加入下面的代码
.catch((err) => {
  console.log(err.message);
})
  • 如果不希望前面的错误导致后续的.then无法正常执行,则可以将.catch的调用往前面提

4、Prmise.all()方法

  • Promise.all()方法会发起并行的Promise异步操作,等所有的异步操作全部结束后才会执行下一步的.then操作(等待机制)。
import thenFs from 'then-fs'
const promiseArr = [
  thenFs.readFile('./files/1.txt' , 'utf8'),
  thenFs.readFile('./files/2.txt' , 'utf8'),
  thenFs.readFile('./files/3.txt' , 'utf8'),
]
// 调用Promise.all 方法
Promise.all(promiseArr).then(result)=>{
  console.log(result);
}
// 输出: ['111' , '222' , '333'] 

5、Promise.race()方法

  • Promise.all()方法会发起并行的Promise异步操作,只要任何一个异步操作完成,就立即执行下一步的.then操作,输出的结果是读取速度最快的那一个
// 调用Promise.race 方法
Promise.race(promiseArr).then(result)=>{
  console.log(result);
}
//输出的结果为执行的最快的那一个

6、基于Promise封装读文件的方法

  • 封装要求:
    • 方法名称定义为getFile
    • 方法接收一个形参fpath,表示要读取的文件的路径
    • 方法的返回值为Promise实例对象
  • 基本定义:
function getFile(fpath){
  return new Promise()
}
  • 如果要创建一个具体的异步操作,则需要在new Promise()构造函数期间,传递一个funtion函数,将具体的异步操作定义到funtion函数内部
function getFile(fpath){
  return new Promise(function(){
    FileSystem.readFile(fpath , 'utf8' , (err , dataStr)=>{
      if(err) return reject(err)  //如果读文件失败,则调用‘失败的回调函数’
      resolve(dataStr)            //如果读文件成功,则调用‘成功的回调函数’
    })
  })
}
  • 获取.then的两个实参
    • 可以使用.then()方法获取成功和失败的回调函数(err , dataStr)
getFile('./files/1.txt').then((r1)=>{console.log(r1);} , (err)=>{console.log(err.message);} )
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@贾鲍鱼[太阳]

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值