Zane Hitchcox
Zane Hitchcox

Reputation: 936

Nested Async/Await function

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.

Upvotes: 4

Views: 3757

Answers (1)

Zane Hitchcox
Zane Hitchcox

Reputation: 936

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
}

Upvotes: 5

Related Questions