- XHR
- $.ajax
- fetch缺点
- 只对网络请求报错,对 400, 500 都当做成功的请求
- 默认不会带 cookie
- 不支持 abort,不支持超时控制
- 没有办法监听请求的进度
- axios特点
- 浏览器和node 环境都可以使用
- 完全支持 promise API
- 支持取消请求,JSON数据转换
- async & await
Promise深入理解高阶函数
发布订阅模式
观察者模式
Promise核心
Generator和实现CO类库
async & await原理
实现完整的 PromiseA+类库
包含 race, all, finally, try等方法

XMLHttpRequest
MDN https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest
const xhr = new XMLHttpRequest()xhr.open('GET', 'http://www.lulongwen.com/api/list')xhr.responseType = 'json'xhr.onload = () => {console.log(xhr.response)}xhr.onerror = () => {console.log('error')}xhr.send()
jQuery $.ajax()
const options = {type: 'POST',url: 'www.lulongwen.com/api/list',data: {},dataType: 'json',success: () => {}error: () => {}}$.ajax(options)
es6 fetch
window.fetch('/api/list').then(res => res.json()).then(res => { console.log(res) }).catch(err => {console.log(err)}).finally(() => this.setState({loading: false}) )
JS异步的发展
callback
- 回调函数来处理异步逻辑
- 把函数当做参数,传入到函数中,形成一个调用栈,依次执行调用
- 缺点:形成箭头代码,层级嵌套太深,形成回调地域
- 回调函数的本质:程序栈,stack;先进先出的原则,一次执行回调函数
- 箭头代码


JQuery
$.ajax().done(data => {}).fail(err => {})$.get()$.post()$.getJSON(url, callback) // JSONP$.when()$.Deferred() // 延迟对象
JSONP
JSONPJSON + padding
Promise
async await
async 函数返回的是一个 Promise 对象
await
