Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实现 如果上一次的没请求完,之后的就无响应 #146

Open
Sunny-117 opened this issue Nov 3, 2022 · 5 comments
Open

实现 如果上一次的没请求完,之后的就无响应 #146

Sunny-117 opened this issue Nov 3, 2022 · 5 comments

Comments

@Sunny-117
Copy link
Owner

No description provided.

@AaronKevinAA
Copy link

let runningTask = 0;
async function callAsyncFunction(promiseFn) {
if(runningTask===0){
console.log("Calling async function...");
runningTask++;
// 将 promiseFn 包装成一个新的 Promise,以便可以在其执行完毕后进行下一步操作
// 返回当前的 promise,以便可以在外部处理其结果
return new Promise(async (resolve, reject) => {
try {
// await不会捕获错误,所以用try...catch
const result = await promiseFn();
resolve(result);
runningTask--;
} catch (err) {
reject(err);
runningTask--;
}
});
}else{
return null;
}
}

@JeromeD3
Copy link

JeromeD3 commented Apr 4, 2023

let isRequesting = false

function myRequest(url) {
if (isRequesting) {
return Promise.reject('请求中')
}

isRequesting = true
return fetch(url)
.then((res) => {
isRequesting = false
return res.json()
})
.catch((err) => {
isRequesting = false
throw err
})
}

myRequest('https://jsonplaceholder.typicode.com/todos/1')
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
})

myRequest('https://jsonplaceholder.typicode.com/todos/5')
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
})

myRequest('https://jsonplaceholder.typicode.com/todos/4')
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
})

myRequest('https://jsonplaceholder.typicode.com/todos/3')
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
})

myRequest('https://jsonplaceholder.typicode.com/todos/2')
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
})

@AgneseLee
Copy link

function promiseWrapper(fetchApi) {
let oldReqPromise = Promise.resolve();
const doRequest = () => {
const newReqPromise = oldReqPromise.then(() => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("data");
}, 1000);
});
});
oldReqPromise = newReqPromise;
return newReqPromise;
};
return doRequest;
}

// 测试用例
const request = promiseWrapper();

request().then(() => {
console.log("第一次请求完成");
});

request().then(() => {
console.log("第二次请求完成");
});

setTimeout(() => {
request().then(() => {
console.log("第三次请求完成");
});
}, 1500);

@tyust512
Copy link

是不是考promise的链式执行?

@topulikeweb
Copy link

// 使用调度器实现,控制并发为1
class Scheduler {
  constructor (maxTask) {
    // 最大并发量
    this.maxTask = maxTask
    // 任务队列
    this.queue = []
    // 当前执行任务的数量
    this.currentTask = 0
  }
  
  // 添加任务
  addTask (task) {
    return new Promise((resolve, reject) => {
      this.queue.push({
        task,
        resolve,
        reject
      })
      this.runTask()
    })
  }
  
  // 执行任务
  runTask () {
    // 未达到最大并发量
    while (this.currentTask < this.maxTask && this.queue.length > 0) {
      const { task, resolve, reject } = this.queue.shift()
      // 当前执行的任务
      this.currentTask++
      task().then(resolve, reject).finally(() => {
        this.currentTask--
        this.runTask()
      })
    }
  }
}

// 示例任务函数
function createTask (time) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve();
    }, time);
  });
}

// 创建调度器实例,设置并发数为2
const scheduler = new Scheduler(1);

function add (time, name) {
  scheduler.addTask(() => createTask(time)).then(() => {
    console.log(name)
  })
}

add(1000, 1)
add(1000, 2)
add(1000, 3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants