To be honest, I don’t know how to set the title of this article more accurately. These two questions are my recent sneak interview encountered, find it interesting, so record, do a share.

Given an instance of a Promise, how to add a timeout to the instance and return a timeout failure.

If a Promise instance takes too long to execute and we want to return a timeout, we can compare it with another instance with a time limit to see who changes the state first. We can use promise.race to solve this problem, so we can implement this API directly:

Let promise1 = new Promise((resolve,reject)=>{setTimeout(()=>{resolve(" succeeded "); }, 2000)}); Let promise2 = new Promise((resolve,reject)=>{setTimeout(()=>{reject(" timeout failed "); }, 1000)}); Promise.race([promise1,promise2]).then((res) => { console.log(res); }).catch((e) => { console.log(e); }); // The timeout failedCopy the code

A bit of a makeover:

Let promise1 = new Promise((resolve,reject)=>{setTimeout(()=>{resolve(" succeeded "); }, 2000)}); function myPromiseTimeOut (promise,time=1000){ let promiseTimeOut = new Promise((resolve,reject)=>{ setTimeout(()=>{ Reject (" timeout failed "); },time) }); Promise.race([promise,promiseTimeOut]).then((res) => { console.log(res); }).catch((e) => { console.log(e); }); } myPromiseTimeOut(promise1); // The timeout failedCopy the code

So this is the general way of thinking about problem number one.

Change promise.all to the resolve state regardless of whether the result of multiple instances is successful or not, and increase the timeout.

PromiseAll (promiseAll) (promiseAll) (promiseAll) (promiseAll) (promiseAll) (promiseAll) (promiseAll) (promiseAll) (promiseAll) (promiseAll) (promiseAll)

Let promise1 = new Promise((resolve,reject)=>{setTimeout(()=>{resolve(" succeeded "); }, 2000)}); Let promise2 = new Promise((resolve,reject)=>{setTimeout(()=>{reject(" failed "); }, 1000)}); function promiseAll(all){ if(Object.prototype.toString.call(all) ! ="[object Array]"){ return Promise.reject("params should be an array") } let res = []; let count = 0; return new Promise((resolve,reject)=>{ for (let i =0; i<all.length; i++){ Promise.resolve(all[i]).then(result=>{ count++; res[i] = result; if(count==all.length){ return resolve(res) } }).catch((e) => { count++; res[i] = e; // return reject(e) if(count==all.length){ return resolve(res) } }) } }) } promiseAll([promise1,promise2]).then((res) . = > {the console log (" success "). The console log (res)}). The catch ((e) = > {the console. The log (" fail ") to the console. The log (e)}) / / success / / / "success", "Failed "]Copy the code

Next, it’s time to change the paomiseAll to add timeout restrictions.

Let promise1 = new Promise((resolve,reject)=>{setTimeout(()=>{resolve(" succeeded "); }, 1000)}); Let promise2 = new Promise((resolve,reject)=>{setTimeout(()=>{reject(" failed "); }, 2000)}); function promiseAll(all,time=1000){ if(Object.prototype.toString.call(all) ! ="[object Array]"){ return Promise.reject("params should be an array") } let promiseTimeOut = new Promise ((resolve, reject) = > {setTimeout (() = > {reject (" timeout "); },time) }); let res = []; let count = 0; return new Promise((resolve,reject)=>{ for (let i =0; i<all.length; i++){ Promise.race([all[i],promiseTimeOut]).then(result=>{ // all[i].then(result=>{ count++; res[i] = result; if(count==all.length){ return resolve(res) } }).catch((e) => { count++; res[i] = e; // return reject(e) if(count==all.length){ return resolve(res) } }) } }) } promiseAll([promise1,promise2]).then((res) . = > {the console log (" success "). The console log (res)}). The catch ((e) = > {the console. The log (" fail ") to the console. The log (e)}) / / success / / / "success", "Out of time "]Copy the code

So that’s the idea behind these two problems.

The Time!