Skip to content

Commit 248d9c0

Browse files
author
Fabrizio Mirabito
authoredOct 16, 2019
Merge pull request #136 from arduino/hotfix-unhandled-rejection
Fixes for unhandled promise rejections
2 parents 24659a6 + ad2a667 commit 248d9c0

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed
 

‎src/index.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,24 @@ const connect = options => new Promise((resolve, reject) => {
168168
}
169169
}
170170
};
171-
171+
172172
client.onConnected = (reconnect) => {
173+
const reconnectPromises = [];
174+
173175
if (reconnect === true) {
174176
// This is a re-connection: re-subscribe to all topics subscribed before the
175177
// connection loss
176178
Object.values(subscribedTopics).forEach((subscribeParams) => {
177-
subscribe(subscribeParams.topic, subscribeParams.cb);
179+
reconnectPromises.push(() => subscribe(subscribeParams.topic, subscribeParams.cb));
178180
});
179181
}
180182

181-
if (typeof opts.onConnected === 'function') {
182-
opts.onConnected(reconnect);
183-
}
183+
return Promise.all(reconnectPromises)
184+
.then(() => {
185+
if (typeof opts.onConnected === 'function') {
186+
opts.onConnected(reconnect);
187+
}
188+
});
184189
};
185190

186191
if (typeof onDisconnect === 'function') {
@@ -307,7 +312,7 @@ const subscribe = (topic, cb) => new Promise((resolve, reject) => {
307312
connection.topics[topic].push(cb);
308313
return resolve(topic);
309314
},
310-
onFailure: () => reject(),
315+
onFailure: error => reject(new Error(`subscription failed: ${error.errorMessage}`)),
311316
});
312317
});
313318

@@ -612,8 +617,10 @@ const onPropertyValue = (thingId, name, cb) => {
612617
if (!propertyCallback[propOutputTopic]) {
613618
propertyCallback[propOutputTopic] = {};
614619
propertyCallback[propOutputTopic][name] = cb;
615-
subscribe(propOutputTopic, cb);
616-
} else if (propertyCallback[propOutputTopic] && !propertyCallback[propOutputTopic][name]) {
620+
return subscribe(propOutputTopic, cb);
621+
}
622+
623+
if (propertyCallback[propOutputTopic] && !propertyCallback[propOutputTopic][name]) {
617624
propertyCallback[propOutputTopic][name] = cb;
618625
}
619626
return Promise.resolve(propOutputTopic);

0 commit comments

Comments
 (0)
Please sign in to comment.