Skip to content

Commit

Permalink
Add example of using releaseLock() to implement a timeout (#167)
Browse files Browse the repository at this point in the history
Fixes #122.
  • Loading branch information
reillyeon authored Jul 19, 2022
1 parent 8527faa commit f5f63d2
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,34 @@ <h2><dfn>readable</dfn> attribute</h2>
with its length or to wait a defined length of time before transmitting
the next message. Implementing a {{TransformStream}} for these types of
message boundaries is left as an exercise for the reader.

<p>
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.

<pre class="js">
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;
}
</pre>

This feature of {{ReadableStreamDefaultReader/releaseLock()}} was added
in <a href="https://github.com/whatwg/streams/pull/1168">whatwg/streams#1168</a>
and has only recently been implemented by browsers.
</aside>

The {{SerialPort/readable}} getter steps are:
Expand Down

0 comments on commit f5f63d2

Please sign in to comment.