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

187
Views
Reusing an existing Promise object if exists

I have a Dialog object that will show, well, dialogs. There are many entry points to show dialogs, e.g. yesNo(), message(), confirm(), etc. However, all these methods basically call the same other method, called showSimpleDialog(title, message, buttons).

I'd like all these methods (showSimpleDialog too) to return a promise, but there's a snag:

yesNo() {
  return new Promise((resolve, reject) => {
    axios
      .get(......)
      .then(this.showSimpleDialog(...));
  }
}

As you can see, I am prevented in the above example from either returning the promise that showSimpleDialog would make or by passing the instanced Promise to showSimpleDialog.

The former is impossible because we're already in a different Promise by the time we have access to it. The latter because the Promise object itself is not yet available within the constructor. Well, technically, in this particular case it is (exactly because we're already in a different Promise), but some of my entry functions are synchronous, some asynchronous and I simply can't use the same code pattern to achieve the same effect in both cases.

I investigated the thing and I found this, but the author suggests the approach is flawed and archaic to begin with.

So, what would be the correct approach to return a functioning Promise from ALL entry points while the entry points would still be free to reusing each other's Promises?

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

0

If I understand correctly, this.showSimpleDialog(...) also returns a Promise, right?

If you want yesNo() to return the Promise retunred by this.showSimpleDialog(...)

yesNo() { 
    return axios
        .get(......)
        .then(()=>{ 
            return this.showSimpleDialog(...);
        });
}

That being said, consider using async/await, especially when dealing with multiple sequential promises, if possible.

about 4 years ago · Juan Pablo Isaza Report

0

Your code is calling this.showSimpleDialog immediately (synchronously) without waiting for any promise to resolve (the axios one). This is because the code doesn't pass a function to the then method, but instead executes this.showSimpleDialog. This execution returns a promise (presumably), but then expects a function as argument, not a promise.

So you need to make sure to pass a callback to then, and let that callback return a promise. This way promises will be chained:

    .then(() => this.showSimpleDialog(...));

It is also important to make that callback an arrow function, since you'll be referencing this, which is intended to be the this on which yesNo is called.

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!