4

It's easier just to look at the code:

async function addFiles(dir,tree) {
  return (await readDir(dir))
    .map(name => {await readDir(dir); return name;}) // error here
}

This code returns an error on line 3, saying there's an unexpected token near readDir. I don't understand why this won't work.

Zane Hitchcox
  • 936
  • 1
  • 9
  • 23

1 Answers1

5

It turns out, I forgot to declare my arrow function as async.

the revised code is

async function addFiles(dir,tree) {
  return (await readDir(dir))
    .map(async name => {await readDir(dir); return name;}) // error here
}
Zane Hitchcox
  • 936
  • 1
  • 9
  • 23
  • hey, for future reference, I am curious what the implementation you are using this for? from "reading it", looks like you are giving a tree structure and reading the directory under it? Just looking for context, as a go to example in the future :-) – james emanon May 02 '16 at 22:40
  • sure thing...I will post it when I finish getting all the bugs out :) – Zane Hitchcox May 04 '16 at 17:04
  • as i understand it this is a suboptimal way to do `map` with `async/await`. if you `await` a `Promise.all`, you utilize a lot more parallel processing. i made a pseudo-code jsfiddle to illustrate using what i think is ur usecase: https://jsfiddle.net/ry93b8r3/ – Brandon Nov 28 '16 at 16:22
  • Could you please accept your own answer? Doing so will help others who read the question in future. Thanks (and upvote ;-) – Mawg says reinstate Monica Mar 15 '22 at 16:47