cancel
Showing results for 
Search instead for 
Did you mean: 

How to make a synchronous request

naotoxxx
Participant
0 Kudos

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 ?

Accepted Solutions (0)

Answers (3)

Answers (3)

former_member560141
Participant

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

naotoxxx
Participant
0 Kudos

Thanks for your response! by the way, what you recommend instead using promises if a request depends of other and you need to show all data at the same time not wait to each input loads the info (be cause many inputs depend of a request) ???

former_member560141
Participant
0 Kudos

@naotoxxx

Sounds like alot of intervals and checking if a object has data. Sounds hard.

https://www.w3schools.com/jsref/met_win_setinterval.asp

personally i dont see why you would not use a promise. http://bluebirdjs.com/docs/why-bluebird.html makes promise runn on Netscape 7......

mvaibhav
Contributor

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

naotoxxx
Participant
0 Kudos

Thanks i'll take a look to this 😉

ericci
Active Contributor
0 Kudos

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

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.

maheshpalavalli
Active Contributor
0 Kudos

deffered objects are supported right?

The example Nicholas gave is still asynchronous.. it just shows how to send a request if the first request is successful, which I think is a normal use case.

ericci
Active Contributor
0 Kudos

Mahesh it will work but not in IE, Promises are not supported by that old browser 😞

maheshpalavalli
Active Contributor
0 Kudos

Ohh.. But i tried it many times and it always used to work.. at least the defferd object. Even the SAP Master Detail template(Old) is using the deffered objects at many places.

boghyon
Product and Topic Expert
Product and Topic Expert

maheshkumar.palavalli ericci Just wanted to say that UI5 comes with a Promise polyfill: https://github.com/SAP/openui5/blob/fdc3334431fbfe1776e884826299f8103499ef9b/src/sap.ui.core/src/sap...

While it's true that IE doesn't support Promises natively, all Promise based APIs in UI5 still work on any platforms thanks to the polyfill.

ericci
Active Contributor
0 Kudos

Well that's pretty cool!

From which version are supported?

Which other ES6 features are added with polyfill?

maheshpalavalli
Active Contributor
0 Kudos

Boghyon Hoffmann, Super!! Thant's why i never got any errors when i used promises also.

boghyon
Product and Topic Expert
Product and Topic Expert
0 Kudos

> Which other ES6 features are added with polyfill?

Since 1.64, quite a lot 🙂 See the commit https://github.com/SAP/openui5/commit/49528d10ffd8f146146675035f611feb43d26621

Complete list of supported APIs from the original lib: https://github.com/paulmillr/es6-shim