[SalesForce] action.setCallback not being executed

I have a issue where in my action.setCallback is not being run.

I already checked the debug logs and the apex methods are not being called

So basically this is what happens
1.) User click submit button.
2.) Button run a application event to call a file upload validation
3.) File Upload validation run – File is valid
4.) once file upload is complete
5.) Will call another event to call a Task creation method

This is my Code

I will just post the important parts

    //helper.submitCycle(component,event);
    var appEvent = $A.get("e.c:PC_Roche_UploadDoc_Start_Event");
    appEvent.setParams({
        "parentId" : component.get("v.cycleRecordId")
    });
    appEvent.fire();
    //helper.submitCycle(component,event);

This event will call this helper

    uploadFile : function(component, event , helper){

    if(component.get('v.isFileValid') != undefined){
        component.set('v.isFileValid', undefined);
    }
    console.log('UPLOAD Files ->');
    var parentId = event.getParam("parentId");

    var masterId = event.getParam("masterId");
    // var masterId = 'a060w000000AcCLAA0';

    var files = component.get('v.files');
    var filesLength = component.get('v.files').length;
    var self = this;

    if(filesLength != 0){
        for(let i=0; i < filesLength; i++){
            let file = files[i];
            var fr = new FileReader();
            var self = this;
            fr.onloadend = function(e) {
                let fileContents = e.target.result;
                let base64Mark = 'base64,';
                let dataStart = fileContents.indexOf(base64Mark) + base64Mark.length;
                fileContents = fileContents.substring(dataStart);
                self.upload(component, file, fileContents,parentId,masterId);
            };
            fr.readAsDataURL(file);
        }
    }
    else{
        this.closeAndRefreshModal(component);
    }
},

    upload : function(component,file,fileContents,parentId,masterId) {

    var self = this;
    self.fileValidation(component,file.name,file.type);
    var action = component.get('c.saveTheChunk');
    console.log('file.name>>>>>' + file.name);
    console.log('parentID ->' + parentId);
    console.log('masterId ->' + masterId);

    action.setParams({
        "parentId": parentId,
        "fileName": file.name,
        "base64Data": encodeURIComponent(fileContents), 
        "contentType": file.type,
        "masterId" : masterId
    });
    console.log('UPLOAD AFTER PARAM -> ');
    action.setCallback(this, function(response) {
        var state = response.getState();
        var isFileValid = component.get('v.isFileValid');
        console.log('ISFILEVALID -> ' + isFileValid);
        if (state === "SUCCESS" && isFileValid != false) {
            console.log('UPLOAD SUCCESS - >');
            //this.closeAndRefreshModal(component);
        }
        else if (state === "ERROR" && isFileValid != false) {
            console.log('ERROR - >>>>>');
            this.throwError(component);
        }
    });
    console.log('ENQUE ACTION 2 -> ');
    $A.enqueueAction(action);
},

fileValidation : function(component,fileName,fileType){
    console.log('FILE VALIDATION -> ');
    console.log('fileName -> ' + fileName);
    console.log('fileType -> ' + fileType);

    var action = component.get('c.fileValidation');

    console.log('action -> ' + action);

    action.setParams({
        "fileName" : fileName,
        "fileType" : fileType
    });

    console.log('AFTER PARAMS -> ');
    alert('Test');
    action.setCallback(this, function(response) {
        var state = response.getState();
        console.log('STATE -> ' + state);
        if (state === "SUCCESS") {
            var isFileNotAllowed = response.getReturnValue();
            console.log('isError -> ' + isFileNotAllowed);
            if(isFileNotAllowed == true){
                var toastEvent = $A.get("e.force:showToast");
                component.set('v.isFileValid', false);
                toastEvent.setParams({
                    title: "Error!",
                    message: "There is a problem in creating online form, Uploaded file is not supported.",
                    type: "error"
                });
                toastEvent.fire();
            }
        }
    });
    console.log('ENQUE ACTION -> 1');
    $A.enqueueAction(action);
},

So basically what happens to me is it gets stuck in the calling of action.setCallback. The Only way for it to run is clicking the submit button again.

Thank you

Best Answer

Fixed this issue using Javascript Promise.

Here are some documentation in regards to the use of javascript promise in Lightning components

https://rajvakati.com/2018/05/29/using-promise-in-lightning-component/ http://bobbuzzard.blogspot.com/2016/12/javascript-promises-in-lightning_30.html