Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 777
Zane Hitchcox Avatar asked Oct 20 '25 17:10

Zane Hitchcox


1 Answers

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
}
like image 94
Zane Hitchcox Avatar answered Oct 23 '25 06:10

Zane Hitchcox