0
    var async = require('asyncawait/async');
    var await = require('asyncawait/await');

Async Function :

      var uploadResponse = async(function() {
        var uploadresult = await (fileUploadFun.reportFileUpload(files,title,name));
        console.log("waiting",uploadresult);  // undefined
        var selection =  db.Sections.create({
          // use uploadresult
        }).then(() => {
          console.log("sections created");
        })
        return 'result';  
      })

      uploadResponse().then(function(result) {
        console.log("upload", result);
        res.redirect("back")
      })

Await function :

 reportFileUpload: function(uploadFileData, title,reportPath) {
 fs.readdir(reportPath, (err, files) => {
      var filenames = files.map(file => file.replace(file, file.split('.')[0]));
      var fileext = files.map(file => file.replace(file, file.split('.')[1]))

      if (filenames.includes(title)) {
        console.log("file exists already");
        var index = filenames.indexOf(title);
        var ext = fileext[index];
        fs.unlink(filepath + title + '.' + ext, (err) => {
          if (err) throw err;
         uploadFileData.mv(filepath + title + extension, function (err) {
            if (err) {
              console.log("error",err);
              return 'error';
            } else {
              return title;
            }
          });
        });
      } 

      else {
          uploadFileData.mv(filepath + title + extension, function (err) {
          if (err) {
            console.log("error",err);
          } else {
            console.log("uploaded");
            return title;
          }
        });
      }
    })
 },

Inside async I need to wait for the result of uploadfile then create table after return response from readdir and upload. But when I console await return it prints undefined and await does not wait for the return and executes other function below

Jamuna
  • 21
  • 5
  • 1
    `await` *only* waits for a promise to be resolved. Your `reportFileUpload` does not return a Promise, so there is nothing to wait for. – VLAZ Apr 17 '20 at 09:01
  • 4
    There's no need to use a module; node natively supports `async` / `await`. –  Apr 17 '20 at 09:02
  • so can you tell the format to make the reportFileUpload return a promise.. since i am new to this promise @VLAZ – Jamuna Apr 17 '20 at 09:07
  • Look at the linked question that has multiple answers with explanations and examples. – VLAZ Apr 17 '20 at 09:08
  • thanks for the link, it worked after using promise @VLAZ – Jamuna Apr 17 '20 at 12:37

0 Answers0