Please help me to understand the following behavior of the Promise class. I have spend a lot of time reading instructions and documentation but can't figure this out by myself.
const promise = new Promise(resolver=>
setTimeout(resolver("i am resolver"), 1000)
)
.then(resolvedParameter => el => {console.log(el); console.log(resolvedParameter)})
.then(upperFunc => upperFunc("log me this"))
.then(() => console.log("do not care about the others"))
.catch(e => (error = e));
promise.then(()=>console.log("test"));
This is the console output:
log me this
i am resolver
do not care about the others
test
In the Promise where setTimeout is used no return value was specified, jet the string "i am resolver" was returned and used in the first then(). I presume that when you call resolver (as a promise resolver function) it returns the passed parameter for the next then()
In the first then() the resolvedParameter is passed in function that returns a function that will be returnd and then used in the second then() which is logical, and the string from the first promise is remembered ("i am resolver") and used correctly.
Can you make a then() function with resolver and rejection function ? Please help me understand what ways do we have available to pass data from first promise to the next function then() and what is expected to be returned from the then() function. (I read and understood that they are new Promises but still the behavior is a bit hard to understand)
Thank you for reading this question and any insight is welcome.
For background information, here are different ways to make a resolved promise:
promise = new Promise(resolver => resolver('any value'));
promise = Promise.resolve('any value');
promise = Promise.resolve().then(() => 'any value');
promise = (async () => 'any value')()
They are functionally equivalent. They all return a promise that is resolved with the value 'any value'.
.then() function gets, as the first argument, whatever is resolved previously up the chain and can return three different ways:
All your examples fall into the first category. Here is a list of values passed down in the resolved promises:
"i am resolver"
el => {console.log(el); console.log(resolvedParameter)}
undefined // because that is what the above function returns when called
undefined // because that is what the console.log returns
undefined // again console.log return value
Here is an example where I have wrapped all your returns values inside promises:
const promise = new Promise(resolver=>
resolver(Promise.resolve("i am resolver"));
)
.then(resolvedParameter => Promise.resolve(el => {console.log(el); console.log(resolvedParameter)}))
.then(upperFunc => Promise.resolve(upperFunc("log me this")))
.then(() => Promise.resolve(console.log("do not care about the others")))
.catch(e => Promise.resolve(error = e));
promise.then(()=>Promise.resolve(console.log("test")));
This functions identically to your example, but hopefully it is clear now what is passed down the chain.
As others have mentioned, the second argument of .then() is called when rejection is passed from the chain. It works the same as the .catch() method. .then(undefined, someFunc) is same as .catch(someFunc).
[1] Strictly, it can also be a "thenable", something that has a then method, but that is a special case.