-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
native promise/async support #1642
Comments
This is already in my TODO list |
@vishnureddy17 @BertKleewein What's the best approach for this in your opinion? Should I create an alias method foreach public method like I was tring adding an override to let toReturn: MqttClient | Promise<void> = this
if (typeof callback === 'function') {
const deferred = new Deferred<void>()
toReturn = deferred.promise
callback = deferred.callback
} where export class Deferred<T> {
private readonly _promise: Promise<T>
private _resolve: (value?: T | PromiseLike<T>) => void
private _reject: (reason?: any) => void
constructor() {
this._promise = new Promise<T>((resolve, reject) => {
this._resolve = resolve
this._reject = reject
})
}
get promise(): Promise<T> {
return this._promise
}
get callback(): GenericCallback<T> {
return (error?: Error, result?: T): void => {
if (error) {
this.reject(error)
} else {
this.resolve(result)
}
}
}
resolve = (value?: T | PromiseLike<T>): void => {
this._resolve(value)
}
reject = (reason?: any): void => {
this._reject(reason)
}
} First problem with this is that if someone doesn't provide a function the method will always return a promise and that could be not expected, in fact many tests fail with that change. IMO the cleaner approach (at code side) is to create an alias method |
I agree. |
What about adding native support to promise/async?
instead of adding another package https://github.com/mqttjs/async-mqtt
The text was updated successfully, but these errors were encountered: