async-mqtt
Promise wrapper over MQTT.js
IMPORTANT: Make sure you handle rejections from returned promises because they won't crash the process
API
The API is the same as MQTT.js, except the following functions now return promises instead of taking callbacks
- publish
- subscribe
- unsubscribe
- end
Example
const MQTT = ; const client = MQTT; // When passing async functions as event listeners, make sure to have a try catch block const doStuff = async { console; try await client; // This line doesn't run until the server responds to the publish await client; // This line doesn't run until the client has disconnected without error console; catch e // Do something about it! console; process; } client;
Alternately you can skip the event listeners and get a promise.
const MQTT = ; { const client = await MQTT console; try await client; // This line doesn't run until the server responds to the publish await client; // This line doesn't run until the client has disconnected without error console; catch e // Do something about it! console; process; }
Wrapping existing client
const AsyncClient = ; const client = ; const asyncClient = client; asyncClient;