Above we wrote Promises that Promise Promises/A+. Here is the link to the article. Because of the length of the promise implementation, the implementation of some of the methods in ES6 are easier to test when it comes to the actual interview (my personal opinion). When writing each method, pay attention to its input and return values, and more importantly, what logic the method implements. The promise approach in ES6 has been sorted out in the previous article. Refer to this article to read this article.

Promise.prototype.catch

Promise.prototype. Catch is equivalent to then(null,rejection).

class Promise{
	//....
    catch(onRejected){
    	 return this.then(null,onRejected)
    }
}
Copy the code

Promise.prototype.finally

class Promise{ //.... finally(onFinally){ onFinally(); return this; }}Copy the code

Promise.resolve

static resolve(value){ if (value instanceof Promise) { return value; } return new Promise((resolve,reject)=>{ resolve(value); })}Copy the code

Promise.reject

static reject(value){ return new Promise((resolve,reject)=>{ reject(value); })}Copy the code

Promise.all

Static all(promiseArr){// Return a new promise instance return new promise ((resolve,reject)=>{var length = promisearr. length var result = []; for(var i = 0; i<length; i++){ let _promise = promiseArr[i]; function handlerResolve(index,item){ result[index] = item; // Use --length to determine whether the promise is complete --length; if(length == 0){ resolve(result); } } Promise.resolve(_promise).then((value)=>{ handlerResolve(i,value); },reason=>{reject(reason) return; })}})}Copy the code

Promise.race

static race(promiseArr){ return new Promise((resolve,reject)=>{ var length = promiseArr.length for(var i = 0; i<length; i++){ let _promise = promiseArr[i]; Promise.resolve(_promise). Then (value=>{resolve(value) return; },reason=>{ reject(reason) return; })}})}Copy the code

Promise.allSettled

static allSettled(promiseArr){ return new Promise((resolve,reject)=>{ var length = promiseArr.length var result = []; for(let i = 0; i<length; i++){ let _promise = promiseArr[i]; function handlerResolve(index,item){ result[index] = item; --length; if(length == 0){ resolve(result); } } Promise.resolve(_promise).then((value)=>{ handlerResolve(i,{status:'fulfilled',value:value}); },reason=>{ handlerResolve(i,{status:'rejected',reason:reason}); })}})}Copy the code

Use JS for Ajax concurrent request control

Another frequently asked question is as follows:

Implement a batch request function multiRequest(urls, maxNum) with the following requirements: • maxNum specifies the maximum number of concurrent requests • Each time a request returns, a space is left for new requests to be added • When all requests are completed, the results are typed in the order in urlsCopy the code

Below I do a code porter, the implementation method is as follows:

Function multiRequest(urls = [], maxNum) {// Total number of requests const len = urls.length; // Create an Array based on the number of requests const result = new Array(len).fill(false); // Let count = 0; Return new Promise((resolve, reject) => {// request maxNum while (count < maxNum) {next(); } function next() { let current = count++; If (current >= len) {if (current >= len) {if (current >= len) { result.includes(false) && resolve(result); return; } const url = urls[current]; Console. log(' start ${current} ', new Date().tolocaleString ()); Fetch (url).then((res) => {// save the request result[current] = res; Console. log(' complete ${current} ', new Date().tolocaleString ()); If (current < len) {next(); }}). The catch ((err) = > {the console. The log (` end ${current} `, new Date (). ToLocaleString ()); result[current] = err; If (current < len) {next(); }}); }}); }Copy the code