function retPromise(str) {
return new Promise(resolve=>{
resolve(str);
})
}
console.log(retPromise("first")) // 返回一个Promise对象
retPromise("x").then(str=>{
return retPromise("first")
}).then(str=>{
console.log(str) // 返回"first"
})
Why is the Promise object returned in then resolved in the next then?
Is the execution chain of the second then the second Promise?
The idea of Promise is to treat all synchronous and asynchronous code as asynchronous code. The then method will return a new
Promise
(链式调用),then方法的第一个参数onfulfilled
是在前一个Promise
object that is called after the asynchronous call is completedThis still involves some implementations inside the constructor. I just implemented a simple Promise a few days ago. Here is the portal. I hope it will be helpful to you
Promise
I am not an expert-_-I will describe my point of view.
Then chain call will take the return value of the previous then as a parameter. The internal implementation of Promise's then function is to perform Promise object processing on the return value. For example, basic data types will directly return Promise objects through Promise.resolve(data). If it is a Promise object, execute its resolve function to trigger the next then function.
You can decompose this then execution chain into:
You can also use setTimeout to set function retPromise(str) to delayed return. This works better