File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -56,6 +56,32 @@ const doStuff = async () => {
5656client .on (" connect" , doStuff);
5757```
5858
59+ Alternately you can skip the event listeners and get a promise.
60+
61+ ``` js
62+ const MQTT = require (" async-mqtt" );
63+
64+ run ()
65+
66+ async function run () {
67+ const client = await MQTT .connectAsync (" tcp://somehost.com:1883" )
68+
69+ console .log (" Starting" );
70+ try {
71+ await client .publish (" wow/so/cool" , " It works!" );
72+ // This line doesn't run until the server responds to the publish
73+ await client .end ();
74+ // This line doesn't run until the client has disconnected without error
75+ console .log (" Done" );
76+ } catch (e){
77+ // Do something about it!
78+ console .log (e .stack );
79+ process .exit ();
80+ }
81+ }
82+
83+ ```
84+
5985## Wrapping existing client
6086
6187``` javascript
You can’t perform that action at this time.
0 commit comments