Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

120
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda