Skip to content

Commit 7ed106b

Browse files
committed
allow .write callback to return a promise that halts writing until later (for eval/etc)
1 parent 1e4084d commit 7ed106b

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

core/serial.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,12 @@ To add a new serial device, you must add an object to
194194
this.closeHandler();
195195
}
196196

197-
/** Call this to send data, this splits data, handles queuing and flow control, and calls writeLowLevel to actually write the data */
197+
/** Call this to send data, this splits data, handles queuing and flow control, and calls writeLowLevel to actually write the data.
198+
* 'callback' can optionally return a promise, in which case writing only continues when the promise resolves
199+
* @param {string} data
200+
* @param {() => Promise|void} callback
201+
* @returns {Promise}
202+
*/
198203
write(data, callback) {
199204
let connection = this;
200205
return new Promise((resolve,reject) => {
@@ -231,15 +236,18 @@ To add a new serial device, you must add an object to
231236
log(2, "Sending "+ JSON.stringify(chunk));
232237
connection.writeLowLevel(chunk).then(function() {
233238
log(3, "Sent");
239+
let promise = undefined;
234240
if (!txItem.data) {
235241
connection.txDataQueue.shift(); // remove this element
236242
if (txItem.callback)
237-
txItem.callback();
243+
promise = txItem.callback();
238244
if (txItem.resolve)
239245
txItem.resolve();
240246
}
247+
if (!(promise instanceof Promise))
248+
promise = Promise.resolve();
241249
connection.txInProgress = false;
242-
writeChunk();
250+
promise.then(writeChunk); // if txItem.callback() returned a promise, wait until it completes before continuing
243251
}, function(error) {
244252
log(1, 'SEND ERROR: ' + error);
245253
uart.writeProgress();

0 commit comments

Comments
 (0)