📜  带有超时 js 的承诺 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:17.685000             🧑  作者: Mango

代码示例2
const promiseWithTimeout = promise => {
  let timeoutId;   
  const timeoutPromise = new Promise((_, reject) => {
    timeoutId = setTimeout(() => {      
      reject(new Error('Request timed out'));    
    }, 4000);  })
  return {
    promiseOrTimeout: Promise.race([promise, timeoutPromise]),
    timeoutId,  };
};