Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
JS Promise class confusing return values in chained behavior

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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:

  1. If it returns a normal value, that is passed down the chain as the resolved value.
  2. If it throws a value, that value is passed down the chain as rejection.
  3. If it returns a promise[1], then what ever that promise resolves or rejects as is passed down the chain as resolved value or rejection.

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!