node js async & await functions – force the function to complete?

I'm trying to upload an image using a component (summernote) in an angular form.

I configured the uploadImagePath endpoint to submit the image to my nodejs backend, but in my code, the outputFileName field is returned empty.

How can I do to ensure that the function completes?

async uploadFile(req) {

    var multiparty = require('multiparty');
    var form = new multiparty.Form();
    let outputFileName = '';

    try {

        form.parse(req, function(err, fields, files) {  
            var imgArray = files.image;

            for (var i = 0; i < imgArray.length; i++) {
                var singleImg = imgArray[i];
                outputFileName = singleImg.originalFilename;
                fs.readFile(singleImg.path , function(err,data) {
                    fs.writeFile('./public/images/'+outputFileName,data, function(err) {
                        if (err) console.log('ERRRRRR!! :'+err);
                        console.log('Fitxer: ' + outputFileName);
    catch (ex) {
        throw ex;

    return { error: false, outputFileName: outputFileName, msg: "File uploaded!" };

Read more here:

Content Attribution

This content was originally published by Tbseven at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: