问题 
-  global.resultArr = {};
 
 -   global.failedArr = [];
 
 -   global.successArr = [];    
 
 -     const writeFile = async function (dirResult, queryResult) {
 
 -   queryResult.recordset.forEach((ip_vch, index) => {
 
 -     for (file of dirResult) {
 
 -       if (
 
 -         file.substring(file.length - 3) == "wav" &&
 
 -         global.failedArr.indexOf(ip_vch.ip_vch) == -1
 
 -       ) {
 
 -         try {
 
 -           writeResult[index] = await timeout(
 
 -             fs.copy(
 
 -               dir + "//" + file,
 
 -               "//" +
 
 -                 ip_vch.ip_vch +
 
 -                 "//RXWaveFiles//DynamicLibraries" +
 
 -                 "//" +
 
 -                 libid +
 
 -                 "//" +
 
 -                 file
 
 -             ),
 
 -             8000
 
 -           );
 
 -           Promise.all([writeResult])
 
 -             .then(function (values) {
 
 -               console.log(values);
 
 -               if (
 
 -                 writeResult &&
 
 -                 global.failedArr.indexOf(ip_vch.ip_vch) == -1
 
 -               ) {
 
 -                 console.log(ip_vch.ip_vch);
 
 -                 global.failedArr.push(ip_vch.ip_vch);
 
 -                 sql.query`update opower..dialers set fileMoveResult_int=0 where ip_vch =${ip_vch.ip_vch}`;
 
 -               } else if (
 
 -                 global.successArr.indexOf(ip_vch.ip_vch) == -1 &&
 
 -                 global.failedArr.indexOf(ip_vch.ip_vch) == -1
 
 -               ) {
 
 -                 global.successArr.push(ip_vch.ip_vch);
 
 -                 sql.query`update opower..dialers set fileMoveResult_int=1 where ip_vch =${ip_vch.ip_vch}`;
 
 -                 console.log("success!" + ip_vch.ip_vch);
 
 -               }
 
 -             })
 
 -             .catch((err) => console.error(err));
 
 -         } catch (error) {
 
 -           console.error(error);
 
 -           if (global.failedArr.indexOf(ip_vch.ip_vch) == -1) {
 
 -             global.failedArr.push(ip_vch).ip_vch;
 
 -             sql.query`update opower..dialers set fileMoveResult_int=0 where ip_vch =${ip_vch.ip_vch}`;
 
 -           }
 
 -         }
 
 -       }
 
 -     }
 
 -   });
 
  
-   global.resultArr.success = successArr;
 
 -   global.resultArr.failed = failedArr;
 
 -   return global.resultArr;
 
 - };
 
  
- // utility function that creates a promise that rejects after a certain time
 
 - function timeoutPromise(t, errMsg = "timeout") {
 
 -     // create possible error object here to get appropriate stack trace
 
 -     let e = new Error(errMsg);
 
 -     e.timeout = true;
 
 -     return new Promise((resolve, reject) => {
 
 -         setTimeout(reject, t, e);
 
 -     });
 
 - }
 
  
- // wrap a promise with a timeout, pass promise, time in ms and 
 
 - // optional timeout error message
 
 - function timeout(p, t, errMsg = "timeout") {
 
 -     return Promise.race(p, timeoutPromise(t, errMsg));
 
 - }
 
  复制代码 
我在 for 循环中使用这个 await 函数,我需要将一些文件从一个源目录复制到多个网络目录,但是 await 的问题是,解决失败的目录需要将近一分钟的时间,然后将控制权交还给下一次迭代,有没有办法在 5 秒后停止当前迭代。 
 
回答 
您可以为以下任何 Promise 添加错误超时: 
- // utility function that creates a promise that rejects after a certain time
 
 - function timeoutPromise(t, errMsg = "timeout") {
 
 -     // create possible error object here to get appropriate stack trace
 
 -     let e = new Error(errMsg);
 
 -     e.timeout = true;
 
 -     return new Promise((resolve, reject) => {
 
 -         setTimeout(reject, t, e);
 
 -     });
 
 - }
 
  
- // wrap a promise with a timeout, pass promise, time in ms and 
 
 - // optional timeout error message
 
 - function timeout(p, t, errMsg = "timeout") {
 
 -     return Promise.race([p, timeoutPromise(t, errMsg)]);
 
 - }
 
  复制代码 
,您可以将它与 fs.copy() 一起使用,如下所示: 
 
const writeResult = await timeout(fs.copy(...), 5000); 
 
因此,如果 fs.copy() 花费的时间超过 5 秒,您正在等待的承诺将被拒绝,您可以在 catch 处理程序中捕获它并采取相应的行动。您将能够看到错误对象具有 .timeout 属性。 
 
它的工作方式是 timeout() 函数在传入的 Promise 和另一个将在超时后被拒绝的 Promise 之间创建竞争。第一个完成的控件控制 Promise.race() 的输出,而后者又控制您在其上使用的 await 的内容。如果超时成功,则 Promise 将被拒绝。 
 
 
 
 |