一次调用多个接口按顺序返回结果


class Scheduler {
    
    constructor(max = 2) {
        // 最大运行数量
        this.maxNum = max
        // 当前运行数量
        this.runNum = 0
        // 事件队列
        this.runlist = []
        this.resolve = null
        // 运行完成的数量
        this.complateNum = 0
    }
    getRes(list, max) {
        this.list = list
        this.maxNum = max
        this.result = []
        return new Promise((resolve, reject) => {
            this.resolve = resolve
            // 如果事件队列中的事件数量为0,直接返回结果
            if (this.list.length === 0) {
                this.resolve(this.result)
                return
            }
            this.runList()
        })
    }
    runList() {
        // 因为最后的结果需要按照顺序返回,所有给每个事件增加一个下标,用来存值
        for (let i = 0; i < this.list.length; i++) {
            this.runlist.push({index: i, cb: this.list[i]})
        }
        this.run()
    }
    run() {
        // 如果当前正在运行的数量小于最大运行数量,并且事件列表中有值
        while (this.runNum < this.maxNum && this.runlist.length) {
            // 从事件列表中取第一项的值然后运行
            let itempro = this.runlist.shift()
            let cb = itempro.cb
            let index = itempro.index
            this.runNum++
            cb.then(res => {
                this.result[index] = res
            }).catch(err => {
                this.result[index] = err
            }).finally(() => {
                this.complateNum++
                this.runNum--
                console.log(this.result)
                // 当所有的事件执行完成以后返回值
                if (this.complateNum == this.list.length) {
                    this.resolve(this.result)
                } else {
                    this.run()
                }
            })
        }

    }




}
let proArr = []

for (let i = 0; i < 100; i++) {
    let randomTime = Math.floor(Math.random() * 10) + 1
    proArr.push(new Promise((resolve) => {
    	//模拟接口调用
        setTimeout(() => {
            resolve('-------'+i+'--------'+randomTime)
        }, randomTime*1000)
    }) )
}

let scheduler = new Scheduler()
scheduler.getRes(proArr, 2).then(res => {
    console.log('res', res)
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在清除缓存后,如果一个页面需要多次调用接口获取 token,你可以使用以下方法来处理: 1. 使用异步请求:确保每个接口调用是异步的,这样可以避免一个接口调用阻塞其他接口的执行。 2. Promise 链式调用:使用 Promise 来管理接口调用顺序。在每个接口请求完成后,再发起下一个接口请求。这样可以确保前一个接口的响应返回后再调用下一个接口。 ```javascript // 使用 Promise 链式调用多个接口 function getToken1() { return new Promise((resolve, reject) => { // 发起 getToken1 接口请求 // 处理成功则调用 resolve(response),否则调用 reject(error) }); } function getToken2() { return new Promise((resolve, reject) => { // 发起 getToken2 接口请求 // 处理成功则调用 resolve(response),否则调用 reject(error) }); } function getToken3() { return new Promise((resolve, reject) => { // 发起 getToken3 接口请求 // 处理成功则调用 resolve(response),否则调用 reject(error) }); } // 调用示例 getToken1() .then((response1) => { // 处理 getToken1 的响应数据 return getToken2(); // 返回一个新的 Promise 对象 }) .then((response2) => { // 处理 getToken2 的响应数据 return getToken3(); // 返回一个新的 Promise 对象 }) .then((response3) => { // 处理 getToken3 的响应数据 // 所有接口请求完成 }) .catch((error) => { // 处理错误情况 }); ``` 通过使用 Promise 链式调用,你可以确保每个接口调用顺序和依赖关系,并在所有接口请求完成后进行处理。 请根据实际情况替换示例中的接口请求代码,并根据需要进行错误处理和数据处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值