Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define Promise As ES6 Class Method [closed]

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

class PromiseMethod {

   promiseMethod = new Promise(function(resolve, reject) {
        return resolve();
   }
}
like image 693
ac360 Avatar asked Oct 18 '15 21:10

ac360


1 Answers

Promises are just an object returned from a function — whether it be a method or not doesn't matter. Try this:

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

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

But maybe you wanted to not have to call the method and be able to use the Promise methods directly? In this case, it's not a method, it's a property:

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

(new Something).promiseProperty.then(...);
like image 55
Félix Saparelli Avatar answered Sep 30 '22 16:09

Félix Saparelli