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();
}
}
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(...);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With