A code question frequently asked in interviews
function increment(value) {
return value + 1;
}
function doubleUp(value) {
return value * 2;
}
function output(value) {
console.log(value);// => (1 + 1) * 2
}
var p = Promise.resolve(1);
p.then(increment)
.then(doubleUp)
.then(output)
analyze:
1. First, the state of p is that the resolve value is 1
2. Because the state of p is resolve, execute the increment function, and the value of increment is 1
The return value is 2 , so the status of increment is resolve , so continue to execute the doubleUp function
3. By analogy, the final output is 4
Common code fill-in-the-blank questions in interviews
function sleep(time){
// 请写出你的代码
}
sleep(2000).then(()=>{
console.log("后续操作")
})
console.log(2);
Effect: First execute output 2 and wait for two seconds before outputting " subsequent operation "
analyze:
Output after two seconds requires a timer to output only once, so use setTimeout
The output is in then, so the state of sleep needs to be resolve
implement the code
function sleep(time){
return new Promise(function(resolve,reject){
// 异步操作,根据执行结果,决定是否调用 resolve,reject
setTimeout(function(){
resolve()
}, time)
})
}
How to judge the return value status of promise
function do1() {
console.log("任务1");
}
function do2() {
console.log("任务2");
}
var p = new Promise((resolve,reject)=>{ resolve()})
p.then(do1)
.catch(do2)
var p1 = p.then(do1)
var p2 = p1.then(do2)
Output result: Task 1
Step 1: Since the state of p is resolved, in p.then(do1), the do1 function will be executed. output任务1
The state of p1 is determined by do1(). Because do1 does not explicitly specify the return value, the return value is undefined. p1
The status is resolved.
Step 2: Next, look at p2. Since the state of p1 is resolved, it will not execute do2, the state of p2 has not changed, and it is still
Keep the status of p1: resolved.
Summarize
1. If the status of p1 is pending, then the status of p2 is also pending
2. If the state of p1 is resolved, then() will execute do2, then the state of p2 is determined by the return value of do2
2.1 If the return value of do2 is not a promise object, the state of p2 is resolved, and the promiseValue of p2 is the do2 function
return value
2.2 If the return value of do2 is a promise object, then the state of p2 and the promiseValue are based on the promise object
2.3 If an error occurs inside the f_ok function (or the user actively throws an error), the status of p2 is rejected, and p2
The promiseValue is this error object
Rejected is consistent with resolved and I won't go into details
If you don't understand the above, you can refer to the following personal understanding:
If the execution function has return, the value is the value returned by return
If the execution function does not return, the value returns an undefined
In both cases the status of the function is resolved
So what is rejected?
If there is an error or the function is set to the rejected state, it will be rejected, otherwise it will not be rejected