# 使用队列解决网络请求慢

背景:某个请求很慢,并且不能同时发送,如何改成串联?

let saveUploadQueue = []
// id队列,也就是 请求队列

let uploading = false
// 是否正在上传 

onSubmit = type => {
  const { id } = this.state;
  saveUploadQueue.push(id)
  if (!uploading) this.onRealSubmit()
  // 如果没在上传,就调用此方法
};

onRealSubmit = () => {
  if (saveUploadQueue.length) {
    const hide = message.loading("保存中...", 0);
    const id = saveUploadQueue.shift() // 弹出队首元素
    uploading = true

    this.props.saveUploadFile({ id }).then(() => {
      message.success("保存成功");
      this.props.onUploadCallback();
      this.onRealSubmit() // 递归调用
        
    }).finally(()=> {
      hide()
      uploading = false
    });
  }
}