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

119
Views
Javascript Promise within Class

Fun with promises today ... I'm trying to figure out why I can't refer to this.promise to resolve it with another method, it's always null.

Any enlightenment is appreciated :)

export default class Example {
  constructor() {
    this.promise = null
  }

  test() {
    this.promise = new Promise((resolve, reject) => {
      this.something()
    }).then(resolve => {
      // resolved, do something else
    }).catch(reject => {
      console.log('reject', reject)
    })
  }

  something() {
    this.promise.resolve()
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You're trying to use this.promise in something before it's value changes from null. Another way to do this is to pass the resolve as an argument:

class Example {
  constructor() {
    this.promise = null;
  }
  test() {
    this.promise = new Promise((resolve, reject) => {
      this.something(resolve);
    }).then(result => {
      console.log('resolve', result);
    }).catch(error => {
      console.log('reject', error);
    });
  }
  something(resolve) {
    resolve(1);
  }
}
const example = new Example();
example.test();

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!