mani
mani

Reputation: 1044

Trying to achieve asynchronous read calls on oData in SAPUI5

I am trying to achieve the asynchronous function calls for reading the odata in SAPUI5.

here is a example:

    this.odataModel = new sap.ui.model.odata.ODataModel(oDataUrl, true, 'user', 'pwd');
   /* data retrieving time 5sec */
    function read1(){
      this.odataModel.read('entity-url1',null, null, true, function(data){
       console.log('success');
        return data;
      },
      function(){ console.log('error'); });
    }
       /* data retrieving time 10sec */
    function read2(){
      this.odataModel.read('entity-url2',null, null, true, function(data){
       console.log('success');
       return data;
      },
      function(){ console.log('error'); });
    }

   /* data retrieving time 10sec */
    function read3(){
      this.odataModel.read('entity-ur3',null, null, true, function(data){
       console.log('success');
       return data;
      },
      function(){ console.log('error'); });
    }
/* function calls */
 var d1 = read1(); 
 var d2 = read2();
 var d3 = read3();

after this call i am trying to get the data from read3 function i am getting empty data. becuse of execution time.

How can i wait for until execution is completed.

Upvotes: 0

Views: 11557

Answers (3)

Ivan
Ivan

Reputation: 98

You can use jQuery or native promise

//jQuery version
function read1() {
  var promise = jQuery.Deferred();
  this.odataModel.read('entity-url1', null, null, true, function(data) {
      console.log('success');
      promise.resolve(data);
    },
    function() {
      console.log('error');
    });

  return promise;
}

var d1 = read1(); // d1 promise;

d1.then(function(data) {
  console.log(data);
});

Upvotes: 1

matbtt
matbtt

Reputation: 4232

You won't see any data because your requests are performed asynchronously! If you want to process the data you have to do this in the success callback of the model's read method. But even with a synchronous call you won't see data, as your methods read1, read2, read3 do not return the data.

Upvotes: 2

mjturner
mjturner

Reputation: 1035

You're seeing this because the sap.ui.model.odata.ODataModel.read() calls are asynchronous by default - while the read in your read1() call is being executed, the Javascript event queue is continuing processing and executing the remainder of your code. This asynchronous execution is a Good Thing, but you need to cater for it in your code.

There are a few solutions, but the easiest is to only initiate the second read once the first has executed and only initiate the third once the second has completed. You can do this by modifying your success handlers.

So, a very crude adjustment to read1():

function read1(){
  this.odataModel.read('entity-url1',null, null, true, function(data){
    console.log('success');
    read2();
    return data;
  },
  function(){ console.log('error'); });
}

Then, do the same for read2() and modify your caller to only call read1(). Of course, you'll need to adjust how you return the data to the caller.

A bit untidy, but I hope the above shows the gist of what I'm trying to describe.

Upvotes: 1

Related Questions