Blogger Information
Blog 77
fans 0
comment 2
visits 55711
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Promise的用法-初
南瓜又个梦
Original
574 people have browsed it

当回调结果不止一个

回调成功和失败都需要接收与处理

  • 回调接收两个参数
    1. fs.readFile('./1.txt', (error, data)=>{
    2. if(error){ console.log(' '); return }
    3. console.log(data.toString()) //
    4. })
  • 搞两个回调
    1. ajax('get','/1.json', data=>{}, error=>{})
    2. ajax('get', '/1.json', {
    3. success: ()=>{}, fail: ()=>{}
    4. })
    5. // 接收一个对象,对象有2个key

    缺点

    1,不规范,体现在不同程序员不同的函数命名和顺序不同
    2,不能在出错的时候捕获错误(很难用try,chach)
    3,容易出现回调地狱

    当开始用Promise

    1. return new primise((resolve,reject)=>{})
  • 我们自己封装的缺点
    • post无法上传数据
      request.send(这里可以上传数据)
    • 不能设置请求头
      request.setRequestHeader(key,value)
  • 解决方法
    用jquery.ajax库
    用axios库
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post