#not sure why this code is erroring

12 messages · Page 1 of 1 (latest)

lament elm
#
for (const number of genNumbers()) {
  const result = await fetchPages(number);
  if (result instanceof Error) break;
  const urls = parse(result);
  await Promise.all(
    urls.map((url) =>
      fetchPage(`https://test.nl${url}`).then(({ name, videos }) =>
        Deno.writeTextFile(name, JSON.stringify(videos))
      )
    )
  );
}
Task generate deno run -A ./scripts/generate.ts
error: Uncaught TypeError: Expected string at position 1
        Deno.writeTextFile(name, JSON.stringify(videos))
             ^
    at writeFile (ext:deno_fs/30_fs.js:807:18)
    at Object.writeTextFile (ext:deno_fs/30_fs.js:850:12)
    at file:///home/al/Code/crookedy/scripts/generate.ts:76:14
    at eventLoopTick (ext:core/01_core.js:183:11)
    at async Promise.all (index 10)
    at async file:///home/al/Code/crookedy/scripts/generate.ts:73:3
vague lichen
#

may have to await the parse(result)?

lament elm
#

no that function is synchrous, and using writeTextFileSync doesn't help either

rotund mason
#

Can you check if you actually get a string for name? The error says that the first argument for writeTextFile is not a string.

lament elm
#

horrible error message

forest trellis
#

If Promise.all() is expecting an iterable of promises, technically what you're returning is a composition of a promise and a chained statement. I don't often use Promise.all but I'm betting that fetchPage is generating the promise and passing the promise to the chained .then() statement, which fails to destructure the name and videos variables.

You probably just need to declare the map callback as async to generate a promise.

lament elm
#

nah, functions only need to be tagged w/ async if im using await inside them. the map converts all the urls Array<string> to Array<Promise<{name:string,videos:Array<Video>}>> hence why i can use promise.all with it

#

the name parameter for Deno.writefile was undefined instead of a string so thats why shit borked

sonic heron
#

That was a poor error message indeed. Might need to open an issue for enhancement.

loud garnet
#

It would be helpful to share the functions you're using inside your snippet.

loud garnet