<p dir="auto">I want SerialPort communication on my application, but WebSerial is not implemented, so I thought I'd use the device file provided by the platform instead.<br>
That's where the problem came...</p>
<p dir="auto">I'm having trouble finding a way to abort reading a device file that never closes infinitely like this.<br>
Even if you forcibly specify the "ResourceID" and close the resource with <code class="notranslate">Promise.race()</code>, <code class="notranslate">setTimeout</code> or <code class="notranslate">std::async::delay()</code>, the "promise runaway" still could not be stopped.<br>
(Looking at <code class="notranslate">Deno.resource()</code> after closing, the resource was successfully closed.)</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import {abortable, delay} from &quot;...std@0.172.0/async/mod.ts&quot;;
import {readAll, writeAll} from &quot;...std@0.172.0/streams/mod.ts&quot;;

const file = await Deno.open(&quot;COM1&quot;);
const ac = new AbortController();

// Successfully written
await writeAll(file, new TextEncoder().encode(&quot;AT\n&quot;));

delay(1000).then(()=&gt;{
    ac.abort();
    file.close();
    // Successfully closed
    console.trace(Deno.resources());
});

// Can't next on from here
const result = await abortable(readAll(file), ac);"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-kos">{</span><span class="pl-s1">abortable</span><span class="pl-kos">,</span> <span class="pl-s1">delay</span><span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">"...std@0.172.0/async/mod.ts"</span><span class="pl-kos">;</span>
<span class="pl-k">import</span> <span class="pl-kos">{</span><span class="pl-s1">readAll</span><span class="pl-kos">,</span> <span class="pl-s1">writeAll</span><span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">"...std@0.172.0/streams/mod.ts"</span><span class="pl-kos">;</span>

<span class="pl-k">const</span> <span class="pl-s1">file</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-smi">Deno</span><span class="pl-kos">.</span><span class="pl-en">open</span><span class="pl-kos">(</span><span class="pl-s">"COM1"</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-k">const</span> <span class="pl-s1">ac</span> <span class="pl-c1">=</span> <span class="pl-k">new</span> <span class="pl-smi">AbortController</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-c">// Successfully written</span>
<span class="pl-k">await</span> <span class="pl-en">writeAll</span><span class="pl-kos">(</span><span class="pl-s1">file</span><span class="pl-kos">,</span> <span class="pl-k">new</span> <span class="pl-smi">TextEncoder</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">encode</span><span class="pl-kos">(</span><span class="pl-s">"AT\n"</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-en">delay</span><span class="pl-kos">(</span><span class="pl-c1">1000</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">then</span><span class="pl-kos">(</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-c1">=&gt;</span><span class="pl-kos">{</span>
    <span class="pl-s1">ac</span><span class="pl-kos">.</span><span class="pl-en">abort</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-s1">file</span><span class="pl-kos">.</span><span class="pl-en">close</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-c">// Successfully closed</span>
    <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">trace</span><span class="pl-kos">(</span><span class="pl-smi">Deno</span><span class="pl-kos">.</span><span class="pl-en">resources</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-c">// Can't next on from here</span>
<span class="pl-k">const</span> <span class="pl-s1">result</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-en">abortable</span><span class="pl-kos">(</span><span class="pl-en">readAll</span><span class="pl-kos">(</span><span class="pl-s1">file</span><span class="pl-kos">)</span><span class="pl-kos">,</span> <span class="pl-s1">ac</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">This is something that can't be helped due to the internal specifications, so is there any other way to solve it than to ask them to add <code class="notranslate">AbortSignal</code> to <code class="notranslate">read()</code> or <code class="notranslate">write()</code> like <code class="notranslate">fetch()</code>?<br>
Or are there other workarounds?</p>
<p dir="auto">This will improve performance</p>