diff --git a/index.html b/index.html index e23dfa6..ab0e4d4 100644 --- a/index.html +++ b/index.html @@ -679,6 +679,34 @@
+ While the {{ReadableStreamDefaultReader/read()}} method is asynchronous + and does not block execution, in code using async/await syntax it can + seem as if it does. In this situation it may be helpful to implement a + timeout which will allow the code to continue execution if no data is + received for a period of time. The example below uses the + {{ReadableStreamDefaultReader/releaseLock()}} method to interrupt a call + to {{ReadableStreamDefaultReader/read()}} after a timer expires. This + will not close the stream and so any data received after the timeout can + still be read later after calling {{ReadableStream/getReader()}} again. + +
+ async function readWithTimeout(port, timeout) { + const reader = port.readable.getReader(); + const timer = setTimeout(() => { + reader.releaseLock(); + }, timeout); + const result = await reader.read(); + clearTimeout(timer); + reader.releaseLock(); + return result; + } ++ + This feature of {{ReadableStreamDefaultReader/releaseLock()}} was added + in whatwg/streams#1168 + and has only recently been implemented by browsers. The {{SerialPort/readable}} getter steps are: