1. Promise. The use of all
-
- The iterable method takes an argument of the promise type and returns a promise
-
- The result of the resolve callback for all promises entered is an array
-
- The callback is executed if the resolve callback for all incoming promises has ended or if there are no promises in the iterable
-
- As soon as any input promise’s reject callback executes or an invalid error is entered, an error is immediately thrown, rejecting the first error message thrown
let p1 = new Promise((resolve, reject) = > {setTimeout(() = > resolve(1), 1000)})
let p2 = Promise.resolve(2)
let p3 = 3
/ / [1, 2, 3]
Promise.all([p1, p2, p3]).then(res= > console.log(res))
// Reject callback exists
let p1 = new Promise((resolve, reject) = > {setTimeout(() = > resolve(1), 1000)})
let p2 = Promise.reject(2)
let p3 = 3
/ / 2
Promise.all([p1, p2, p3]).then(res= > console.log(res), err= > console.log(err))
Copy the code
Once you know how Promise.all is implemented, you can hand write an implementation method of promise. the source code is shown below
Promise.prototype._all = function (promises) {
// Return a promise
return new Promise((resolve, reject) = > {
// An iterable object is passed
if(!Array.isArray(promises)) {
throw new TypeError('promises is not iterable')}// Result is used to store the result of each promise, and count is used to count the number of worthwhile promises obtained
let result = [], count = 0
promises.forEach((promise, index) = > {
promise.then(res= > {
result[index] = res
count++
count === promises.length && resolve(result)
}, err= > reject(err))
})
})
}
Copy the code
2. Promise. The use of race
-
- Parameter: iterable
-
- Return value: a Promise As soon as a Promise in a given iteration is resolved or rejected, the value of the first Promise is taken as its value and thus resolved or rejected asynchronously (once the stack is empty)
Promise. The use of the race
let p1 = new Promise((resolve, reject) = > {setTimeout(() = > {resolve(1)}, 2000)})
let p2 = new Promise((resolve, reject) = > {setTimeout(() = > {resolve(2)}, 1000)})
Promise.race([p1, p2]).then(res= > console.log(res), err= > console.log(err))
Copy the code
After you know how to use promise.race, you can implement promise.race
Promise.prototype._race = function (promises) {
return new Promise((resolve, reject) = > {
if(!Array.isArray(promises)) {
throw new TypeError('promises is not iterable')}// Issue is used to determine whether a promise has been executed
let issue = true
promises.forEach(promise= > {
promise.then(res= > {
if(issue) {
issue = false
resolve(res)
}
}, err= > reject(err))
})
})
}
Copy the code
3. Promise implements the compose function
Well, first of all, what is the compose function? Eg: a(b(c(d(‘XXX’)))) const func = compose(a, b, c, d) func(‘XXX’))
-
- Implementation Solution 1
function add1(x) {
console.log("===>add1");
return x + 1
}
function add2(x) {
console.log("===>add2");
return x + 2
}
function add3(x) {
console.log("===>add3");
return x + 3
}
const add4 = (. args) = > args.reduce((pre, cur) = > pre + cur)
function compose(. fns) {
let length = fns.length
let count = length - 1
let result
return function fun(. arg) {
result = fns[count].apply(this, arg)
if(count <= 0) {
count = length - 1
return result
}
count--
return fun.call(null, result)
}
}
let func = compose(add1, add2, add3, add4)
/ / 16
console.log(func(1.2.3.4));
Copy the code
-
- Implementation Solution 2
function compose(. fns) {
return x= > fns.reduce((promise, fn) = > promise.then(fn), Promise.resolve(x))
}
let func = compose(add3, add2, add1)
/ / 10
func(4).then(res= > console.log(res))
Copy the code
-
- Implementation Solution 3
async function compose(. fns) {
return async x => {
for(const fn of fns) {
x = await fn(x)
}
return x
}
}
let func = compose(add3, add2, add1)
// 10: The first res is the return value of an asynchronous function called compose
func.then(res= > res(4)).then(res= > console.log(res))
Copy the code