关于vue异步接口请求
方式一getData: async function() { // 异步方法try { // 顺序请求await this.getSetupList();await this.getRoleList();await this.getList();} catch (e) {}}方式二login: function(code) { // 定义方法return new Promise((resolve,
·
方式一
getData: async function() { // 同步方法
try { // 顺序请求
await this.getSetupList();
await this.getRoleList();
await this.getList();
} catch (e) {}
}
方式二
const getData1 = function(code) { // 定义方法
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(11)
resolve(true)
}, 1000)
})
}
const getData2 = function(code) { // 定义方法
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(22)
resolve(true)
}, 1000)
})
}
const getData3 = function(code) { // 定义方法
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(33)
resolve(true)
}, 1000)
})
}
getData1().then(() => {
return getData2()
}).then(() => {
return getData3()
}).then(() => {
console.log('结束')
})
// 并发
Promise.all([
getData1(),
getData2()
]).then(result => {
console.log(result)
})
更多推荐
已为社区贡献5条内容
所有评论(0)