Define a promise as an ES6 class method

Using ES6 syntax to define a class and its methods, how can I define a method as a promise without wrapping it in a regular function that returns a promise? I want to do the following:

class PromiseMethod {

   promiseMethod = new Promise(function(resolve, reject) {
        return resolve();
   }
}
+4
source share
1 answer

Promises is just the object returned by the function - whether it is a method or not, it does not matter. Try the following:

class Something {
  promiseMethod () {
    return new Promise(...);
  }
}

(new Something).promiseMethod().then(...)

But maybe you didn’t have to call a method and be able to use Promise methods directly? In this case, this is not a method, this property:

class Something {
  constructor () {
    this.promiseProperty = new Promise(...);
  }
}

(new Something).promiseProperty.then(...);
+3
source

Source: https://habr.com/ru/post/1612160/


All Articles