Glittering's blog Glittering's blog
Home
  • 学习手册

    • 《JavaScript教程》
    • 《ES6 教程》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
  • 技术文档
  • 算法
  • 工作总结
  • 实用技巧
  • collect
About
  • Classification
  • Label
GitHub (opens new window)

Glitz Ma

前端开发工程师
Home
  • 学习手册

    • 《JavaScript教程》
    • 《ES6 教程》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
  • 技术文档
  • 算法
  • 工作总结
  • 实用技巧
  • collect
About
  • Classification
  • Label
GitHub (opens new window)
  • 技术文档

  • 算法

  • 工作总结

    • 时区校正
    • 上传下载文件方式总结
    • webpack优化实践
    • webpack基础应用知识总结
    • vue常见原理总结
    • vue基础知识总结
    • react高级特性
    • react基础知识总结
    • 微前端总结
    • 今天总结一下用到的css吧
    • 地图标绘--射线法来计算点在多边形内
    • web异常监控和分析
    • 工作中遇到的css问题记录
    • axios 与 promise
      • axios
      • promise 用法文档
        • promise
        • 多个请求并列
        • N个并发请求
      • 执行多个并发请求
      • fetch操纵http管道
      • axios、fetch、ajax(jquery)
    • 前端优化指南
    • 小程序笔记
    • JS随机打乱数组
    • 非常实用的JavaScript一行代码
  • 实用技巧

  • 收藏夹

  • 技术
  • 工作总结
mamingjuan
2018-06-07
目录

axios 与 promise

# axios

axios是基于promise的http库,可以用于浏览器和node.js中

  • 从浏览器中创建 XMLHttpRequests
  • 从 node.js 创建 http 请求
  • 支持 Promise API
  • 拦截请求和响应
  • 转换请求数据和响应数据
  • 取消请求
  • 自动转换 JSON 数据
  • 客户端支持防御 XSRF

# promise 用法文档

http://liubin.org/promises-book/#chapter1-what-is-promise (opens new window)

https://developers.google.com/web/fundamentals/primers/promises?hl=zh-cn (opens new window)

https://developers.google.com/web/fundamentals/primers/promises?hl=zh-cn#promise-api-reference (opens new window)

# promise

# 多个请求并列

async tidyData(){
    for(let item of itemList){ // itemList存放axios请求
        let result = await item; 
        // TODO 其它事情....
    }
}

// 或者
var asyncIterable = {
  [Symbol.asyncIterator]() {
    return {
      i: 0,
      next() {
        if (this.i < 3) {
          return Promise.resolve({ value: this.i++, done: false });
        }

        return Promise.resolve({ done: true });
      }
    };
  }
};
(async function() {
   for await (num of asyncIterable) {
     console.log(num);
   }
})();
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

# N个并发请求

帮助函数处理并发请求。

axios.all(iterable)
axios.spread(callback)
1
2
class Scheduler {
  constructor(num = 2) {
    this.count = 0 // 记录当前正在执行的任务数
    this.todoList = [] // 待执行的任务列表
    this.num = num // 并发最大数:最多可同时执行的任务个数
  }

  // 入列
  add(promiseCreator) {
    if(this.count < this.num) {
      this.count ++
      promiseCreator().then(() => this.dequeue())
    } else {
      this.todoList.push(promiseCreator())
    }
  }

  // 出队方法
  dequeue() {
    this.count --
    if(this.todoList.length > 0) {
      this.todoList.shift().then(() => this.dequeue())
    }
  }
}

const timeout = (time) => new Promise(resolve => {
  setTimeout(resolve, time)
})

const scheduler = new Scheduler()
const addTask = (time, order) => {
  scheduler.add(() => timeout(time).then(() => console.log(order)))
}

addTask(1000, '1')
addTask(500, '2')
addTask(300, '3')
addTask(100, '4')
addTask(400, '5')
addTask(200, '6')
addTask(300, '7')
// 2 3 4 1 6 5 7

// addTask(1000, '1')
// addTask(500, '2')
// addTask(300, '3')
// addTask(400, '4')
//  2314
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

# 执行多个并发请求

function getUserAccount() {
  return axios.get('/user/12345');
}

function getUserPermissions() {
  return axios.get('/user/12345/permissions');
}

//注:axios.spread 用于拆分请求结果的。
//promise.all会返回一个数组

axios.all([getUserAccount(), getUserPermissions()])
  .then(axios.spread(function (acct, perms) {
    //两个请求现在都执行完成
  }));

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# fetch操纵http管道

fetch({
  url: 'https://m.douban.com/rexxar/api/v2/movie/电影ID/interests?start=0&count=10&order_by=time'
}).then((res) => {
  console.log(res, '开心');
});

1
2
3
4
5
6

# axios、fetch、ajax(jquery)

axios与fetch是基于promise形式,ajax是callback形式
fetch 脱离了xhr是新语法,默认不传cookie,不能像xhr监听请求进度

上次更新: 2025/04/07, 01:42:58
工作中遇到的css问题记录
前端优化指南

← 工作中遇到的css问题记录 前端优化指南→

Copyright © 2015-2025 Glitz Ma
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式