当分批请求同一个promise时,通常会导致嵌套,怎么样有序的去写多次请求
const list = API.partsList(condition)
list.then(res => {
this.exportList2= res.data.data
return API.partsList(condition2)
}).then(res => {
return API.partsList(condition3)
}).then(res => {
this.exportList3 = res.data.data
return API.partsList(condition4)
}).then(res => {
this.exportList4 = res.data.data
}).finally(() => {})
const 一个promise对象 结束时return一个promise对象 就可以在后面继续写.then
Promise.prototype.then()
Promise实例具有
then
方法,也就是说,then
方法是定义在原型对象Promise.prototype上的。它的作用是为Promise实例添加状态改变时的回调函数。
then
方法的第一个参数是Resolved状态的回调函数,then方法的第二个参数(可选)是Rejected状态的回调函数。
then
方法返回的是一个新的Promise实例(注意,不是原来那个Promise实例)。因此可以采用链式写法,即then
方法后面再调用另一个then
方法。Promise.prototype.catch()
Promise.prototype.catch
方法是.then(null, rejection)
的别名,用于指定发生错误时的回调函数。