-1

I have custom serialize function : name is serializeForm(). I have switch case in it to check datatype and according to type convert value and then send json object.

serializeForm: function (form , includeEmpty) {
        var inputs = form.find('input,select,textarea');
        var object = {};
        if (!inputs.length) {
            return null;
        }
        for (var i = 0; i < inputs.length; i++) {
            var input = inputs[i];
            var name = input.getAttribute('name');
            var value = input.value;
            var type = input.dataset.type;
            switch (type) {
                case "text":
                     value = input.value;
                    break;
                case "multiline":
                    value = JSON.stringify(input.value).replace(/\"/g, "");
                    break;
                case "list":
                    value = input.value.split(',').filter(Boolean);
                    break;
                case "file":
                    var file = App.Utils.getBase64(input.files[0]);
                    file.then(function (data) {
                        console.log(data);
                        //value = data;
                    });
                    break;
                case "date":
                    value= input.value ? moment.utc(input.value).format('YYYY-MM-DDThh:mm:ss.SSSZ') : null;
                    break;
                default:
                    value = input.value;
                    break;
            }
            if (includeEmpty || (!includeEmpty && value !== "" && value !== null && value.length !== 0)) {
                object[name] = value;
            }
        }
        return object;
    },

Note: in above function in case "file" , I am getting data using below function ,which working fine.

getBase64 : function(file) {
        return new Promise((resolve, reject) => {
            const reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload = () => resolve(reader.result);
            reader.onerror = error => reject(error);
        });
    },

Issue : in case "file" , getting base64 value in "data" i.e. console.log(data); but when I am trying to put data into value i.e. //value = data; , its not working . because while I am initializing var value = input.value; its taking input.value but not data.

I think this would be solve by promiseall(), but I don't know how to convert this code into promiseall() or any other way would be appreciable.

Plz suggest . Thank you in advance.

jsBee
  • 413
  • 3
  • 13

1 Answers1

0

Promise is asynchronous. The current implementation of serializeForm is synchronous. You could use async/await and chain .then() to serializeForm() function. Note also that a value should be returned from .then() to get the value at subsequent chained .then(), see Why is value undefined at .then() chained to Promise?

async function serilizeForm(form , includeEmpty) { 
  switch(type) { 
    ..
    case "file": 
      value = await App.Utils.getBase64(input.files[0]);  break; 
    ..
  } 
  return value; 
} 

serializeForm(inputA, inputB)
.then(data => console.log(data));
guest271314
  • 1
  • 15
  • 104
  • 177