Skip to content

Commit 2387c18

Browse files
committed
Added connectAsync docs
1 parent b937299 commit 2387c18

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,32 @@ const doStuff = async () => {
5656
client.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

0 commit comments

Comments
 (0)