DenoDDeno
Powered by
vwkdV
Denoβ€’2y agoβ€’
3 replies
vwkd

Streaming download throws `Bad resource ID`

Anyone have an idea why this errors when closing the file even though the download was successfully written to the file?

deno 1.45.2

const res = await fetch(url);

if (!res.ok) {
  throw new Error(`Got error ${res.status} ${res.statusText}`);
}

const body = res.body;

if (!body) {
  throw new Error(`Got no body`);
}

const file = await Deno.create(path);

try {
  await body.pipeTo(file.writable);
} catch (e) {
  await Deno.remove(path);
  throw new Error(`Download interrupted ${e}`);
} finally {
  file.close();
}
const res = await fetch(url);

if (!res.ok) {
  throw new Error(`Got error ${res.status} ${res.statusText}`);
}

const body = res.body;

if (!body) {
  throw new Error(`Got no body`);
}

const file = await Deno.create(path);

try {
  await body.pipeTo(file.writable);
} catch (e) {
  await Deno.remove(path);
  throw new Error(`Download interrupted ${e}`);
} finally {
  file.close();
}


error: Uncaught (in promise) BadResource: Bad resource ID
    file.close();
         ^
    at FsFile.close (ext:deno_fs/30_fs.js:743:10)
    at loadFile (file:///foobar/loadFile.ts:142:16)
    at eventLoopTick (ext:core/01_core.js:168:7)
    at async file:///foobar/main.ts:42:7
error: Uncaught (in promise) BadResource: Bad resource ID
    file.close();
         ^
    at FsFile.close (ext:deno_fs/30_fs.js:743:10)
    at loadFile (file:///foobar/loadFile.ts:142:16)
    at eventLoopTick (ext:core/01_core.js:168:7)
    at async file:///foobar/main.ts:42:7
Deno banner
DenoJoin
Chat about Deno, a modern runtime for JavaScript and TypeScript.
20,944Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Bad resource ID ERROR
nekodendislikeNnekodendislike / help
14mo ago
Why Bad resource ID occurs?
jeieaJjeiea / help
3y ago
error: BadResource: Bad resource ID
v1rtlVv1rtl / help
3y ago
bad resource id with node:crypto
lyLly / help
3y ago