1

I am trying to run some async code in function(1) but I am unable to get the output of the function(1), which I need to use as input for function(2). it returns 'undefined. however, if I put a breakpoint at the return statement of function(1) it starts working & stops again when I remove the breakpoint. I'd appreciate if anyone can help me with this.

oFile = oEvent.getParameter("files")[0];
var myFunc = function(){
    return new Promise(function(resolve, reject) {
      return resolve(S4HanaFioriAccelerator.common.handleListResponse(oFile));
    });
};
myFunc().then(function success(data) {
    console.log(data);
});



handleListResponse: function(file) {
var reader = new FileReader();

reader.onload = function(e) {
    data = e.target.result;
    results = [];
    cfb = XLSX.read(data, {
        type: 'binary'
    });

    cfb.SheetNames.forEach(function(sheetName) {
        sCSV = XLS.utils.make_csv(cfb.Sheets[sheetName]);
        oJS = XLS.utils.sheet_to_json(cfb.Sheets[sheetName]);
        for(var i= 0; i < oJS.length; i++){
            results.unshift(oJS[jsRowCount].Data);
        }
        console.log(results);
    });
};
reader.readAsBinaryString(file);
setTimeout(function(){
    console.log("Hello"); 
    return results;
}, 3000);       
}

output in console: undefined file1.js ["Student1", "Student2"] file2.js Hello file2.js

expected output: ["Student1", "Student2"] file2.js Hello file2.js ["Student1", "Student2"] file1.js

Armghn
  • 144
  • 2
  • 14
  • What does `myNameSpace.common.handleListResponse()` look like? Does it return a Promise, or an Object/Array? What is `oEvent`? – Luca Kiebel Apr 22 '18 at 12:13
  • Hi Luca, no, it's a simple function, returns an array. how should I return promise from that function? (if returning promise is mandatory) – Armghn Apr 22 '18 at 12:17
  • And it's asynchronous? You should read about promises at you favorite place to read about parts of JavaScript ;) – Luca Kiebel Apr 22 '18 at 12:17
  • yes, it makes an async call to read data from an uploaded file – Armghn Apr 22 '18 at 12:18
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Luca Kiebel Apr 22 '18 at 12:19
  • I am getting response if I put a breakpoint, so the post you shared is different from my case. – Armghn Apr 22 '18 at 12:23
  • could you share the code myNameSpace.common.handleListResponse() ; does it generate promise or waht ? – Bourbia Brahim Apr 22 '18 at 12:26
  • what does `console.log(myNameSpace.common.handleListResponse)` output. – Sumit Jha Apr 22 '18 at 12:28
  • console.log(myNameSpace.common.handleListResponse) prints an array: ["Studen1","Student2"] (that is,only when I put a preakpoint at the return statement of myNameSpace.common.handleListResponse) – Armghn Apr 22 '18 at 12:32
  • I want the the signature of the function not the results. Can you not create a minimal working example of `myNameSpace.common.handleListResponse`. – Sumit Jha Apr 22 '18 at 12:40
  • Hi Sumit, I am using cloud flare framework to read excel data & convert it into an array. myNameSpace.common.handleListResponse(oEvent) is the function where I am reading the uploaded excel file. – Armghn Apr 22 '18 at 13:07

1 Answers1

2

You can make your function async by returning a promise that will, when completed return data from your function. Then you can catch an resolve you data below.

var myFunc = function() {
    return new Promise(function(resolve, reject) {
        var data = myNameSpace.common.handleListResponse(oEvent);
        return resolve(data);
    });
};

myFunc().then(function success(data) {
    console.log(data);
});
Mirko Acimovic
  • 508
  • 2
  • 9
  • thanks for your help Mirko, I tried the sample that you provided but I still am getting 'undefined' without break point & expected array with break point. – Armghn Apr 22 '18 at 13:03
  • Can you supply code for function of yours that returns a list ? – Mirko Acimovic Apr 22 '18 at 13:06
  • I have edited my post, it is having all the code that I am using in the app. I still can't understand why is it still printing 'undefined', while putting a console.log in 'handleListResponse' shows that values are generated as expected – Armghn Apr 22 '18 at 13:34
  • FileReader is also async by itself. Try this please, replace it in code : return resolve(myNamespace.common.handleListResponse(evt1)); – Mirko Acimovic Apr 22 '18 at 13:47
  • tried, still the same. code is exactly the same as I have shared in the post. I am out of ideas on this, I tried a lot of things but I still am unable to make it work. – Armghn Apr 22 '18 at 14:19