-
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
feat: promises support #1644
feat: promises support #1644
Conversation
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## main #1644 +/- ##
==========================================
- Coverage 81.23% 81.09% -0.14%
==========================================
Files 21 21
Lines 1327 1365 +38
Branches 318 324 +6
==========================================
+ Hits 1078 1107 +29
- Misses 175 181 +6
- Partials 74 77 +3
☔ View full report in Codecov by Sentry. |
@vishnureddy17 this is ready for review. I sincerly have no clue why there is a very simple test that fails only on node 20 and I cannot reproduce it on my local instance, it only happens on CI 🤷🏼♂️ Line 560 in c950c06
UPDATE Seems to work now. Using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, except for my thought on connectAsync
. Marking as approved so you can judge whether it's important to change that or not.
@@ -177,4 +181,64 @@ function connect( | |||
return client | |||
} | |||
|
|||
function connectAsync(brokerUrl: string): Promise<MqttClient> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unsure about connectAsync
. I feel like all the async functions should just be simple "promisification" of a callback-style function, but connectAsync
is doing more than that. Maybe have an equivalent function in callback style?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe pick a different name for connectAsync
to make it clear that it's doing something different from connect
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
connect
doesn't accept a callback so a 'promisified' version of it just makes no sense.
connectAsync
does much more because it returns a client just when it actually connects and rejects if there is a connection error, that's how most users expect it to work and what async-mqtt
does: https://github.com/mqttjs/async-mqtt/blob/master/index.js#L144
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe pick a different name for connectAsync to make it clear that it's doing something different from connect
How would you call it? I tried to keep the compatibility for users using async-mqtt
, now they don't need that library anymore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I'm actually struggling to think of a name.
I think the problem is that we use connect()
to instantiate a client, but I think a better API would have the user call the MqttClient
constructor and then call connect()
or connectAsync()
which would call the callback or resolve with a CONNACK
instead of the client.
Since we're trying to keep the API close to the v4 API, I think we should just keep what's here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok so I'm going to merge this for now and I think I will release a stable v5 after this. What do you think? So we will get more feedbacks
Fixes #1642