0

I want the program to wait till getFiles returns the array and it should get concatenated before printing array named totalProjectFiles.

let totalProjectFiles = []
let projectDir = './defaults/'

    function getFiles (dir, files_){
        files_ = files_ || [];
        var files = fs.readdirSync(dir);
        for (var i in files){
            var name = dir + '/' + files[i];
            if (fs.statSync(name).isDirectory()){
                getFiles(name, files_);
            } else {
                files_.push(name);
            }
        }
        return(files_)
    }




function ff (){
  const a = getFiles(projectDir)
  totalProjectFiles.concat(a)

  console.log(totalProjectFiles)
}

ff()
  • please note that getFiles() doesn't return a promise. It's just a normal function that returns an array. –  Apr 10 '20 at 12:33
  • 3
    Does this answer your question? [How to return an Ajax result using async/await?](https://stackoverflow.com/questions/48506445/how-to-return-an-ajax-result-using-async-await) – messerbill Apr 10 '20 at 12:35
  • that would be the default behavior of js if getFiles isn't asynchronous. What are you seeing? You might want to share that function with us. – sdotson Apr 10 '20 at 12:35
  • 1
    @HariKrishnan That comment should be part of the question. Please [edit] your question to include it. – Heretic Monkey Apr 10 '20 at 12:35
  • 1
    @messerbill Read the comment right above yours. – Heretic Monkey Apr 10 '20 at 12:36
  • @HereticMonkey I have edited the question, can you please help. –  Apr 10 '20 at 12:50
  • You've given us very little to go on, and have changed the question from its original, which is considered bad form. That said, when I run this in node, adding the appropriate `const fs = require('fs');` to it, it runs fine. – Heretic Monkey Apr 10 '20 at 12:57

4 Answers4

0

Almost there. I would recommend checking the docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

You just need an await

async function ff (){
  const a = await getFiles(projectDir)
  totalProjectFiles.concat(a)

  console.log(totalProjectFiles)
}
sdotson
  • 790
  • 3
  • 13
0
let totalProjectFiles = []

async function ff (){
  const a = await getFiles(projectDir)
  totalProjectFiles.concat(a)

  console.log(totalProjectFiles)
}

ff()
Shing Ho Tan
  • 931
  • 11
  • 30
  • 2
    getFiles() doesnot return a promise. It's just a normal function, that return an array. –  Apr 10 '20 at 12:37
  • 1
    Please don't post only code as an answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually of higher quality, and are more likely to attract upvotes. – Mark Rotteveel Apr 10 '20 at 18:18
0

please add await inside to async fuction

let totalProjectFiles = []

async function ff (){
  const a = await  getFiles(projectDir)
  totalProjectFiles.concat(a)

  console.log(totalProjectFiles)
}

ff()
Jadli
  • 858
  • 1
  • 9
  • 17
0

Concat return a new array not mutate it assign it back

function ff (){
  const a = getFiles(projectDir)
  totalProjectFiles = totalProjectFiles.concat(a)

  console.log(totalProjectFiles)
}
  • 1
    still its printing a blank array [ ] , but a is receiving the value. –  Apr 10 '20 at 12:42
  • There is a reason why its not working. `totalProjectFiles.concat(a)` will return a new array not mutate totalProjectFiles and so you are getting empty array so use `totalProjectFiles = totalProjectFiles.concat(a)` and issue solved – Prabhat Gupta Apr 10 '20 at 13:18