Skip to Content

How to make a synchronous request

Hi i did a request bus others request i want to receive in order like
main request > if it's ok i made other request > if the previous request was success i made a last request

i was reading and odata v2 is deprecated, with odata v4 could i do this ? or is there any way to make a function with as many requestas i need and send them all and just when all this request are ok keep executing the rest of my code ?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Jan 04 at 04:08 AM

    Hi Naoto,

    Not sure where you read, odata v2 is not deprecated as of now.

    However odata v4 is the latest version available.

    You can use promise to chain your requests and execute your code if all are successful.

    UI5 Odata Promises

    Thanks,

    Vaibhav Maheshwari

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 04 at 07:31 AM

    Hello. You can do this with a promise. I would suggest not to make a synchronous call. It may lock the app until the call is done.


    Promise:

    getSomething:function(){
    //"sap/ui/model/odata/v2/ODataModel"
    var oModel = new ODataModel(params);
    var urlParameters = {} //If you need some params
    return new Promise(function (resolve, reject) {
    oModel.read(sPath, { urlParameters: urlParameters, success: function (data) { resolve(data); }, error: function (err) { reject(err); } });
    }
    }

    Call it and wait for the response

    this.getSomething().then(function(result){
    //Things are ok and do something with the data
    console.log(result);
    }).catch(function(err){
    //something went wrong. the reject in the promise has runn.
    console.log(err)
    });

    This is the easiest way of doing this. If you want some better code, you can also write return result; and add another then(). That is where it will runn.

    To make it run in IE add the bluebird lib

    http://bluebirdjs.com/docs/getting-started.html

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 04 at 08:48 AM

    Nicholas Promises are not supported by IE so I don't know if it's a good use case :D

    Anyway, I need to adivce you: never ever do synchronous requests. They are a really bad habit and there's not a good side about it.

    Add comment
    10|10000 characters needed characters exceeded