Skip to main content
SAPUI5

Dev Tip: Stitching together disparate OData calls using Promises.

PM Paul J. Modderman
2 min read
In case you’ve missed our dev tips in past weeks, we will be posting them in the blog moving forward. To sign up and get your dev tips delivered to your inbox each week, click here!

Tip of the Week

Stitching together disparate OData calls using Promises.

You’re in the zone. The groove. Making an SAPUI5 app that is just the bee’s knees. The cat’s meow. It’s got all the pieces that the end-user wants, with a design that knocks their socks off. Dropping tight functions like they’re no big deal. The result is a beautiful, fast, slick app. Everyone loves it. Promotions all around!

And then your end-user champion approaches you with that look in his eye. That look that says “I’m about to ask you something that I think you’ll hate”. He tells you that on the item entry screen, when users select “Non-billable expense”, they need to see information that, for legal reasons, requires three separate calls to different OData APIs. Only after stitching these calls together on the client side can users truly get the best information for making critical decisions.

So you need to call three different OData APIs and wait until they all return before proceeding? And your end-user champion thought this would be hard?

You smile confidently, and surf reddit.com for an extra half hour. Because with Promises, you’ve got this.

Promise.all([oSecretModel.read(“/SecretSet”),

                  oConfidentialModel.read(“/ConfidentialSet”),

          oHushHushModel.read(“/HushHushSet”)]

).then(values => { doLegalStuff(values); });

With those couple lines of code, you set up a group of asynchronous calls to the three OData APIs. Javascript doesn’t care which call resolves first, it just waits until all three have resolved before proceeding to the .then() section. This way you also get a small performance boost: the runtime is not the sum of the three calls, it’s the time of the single longest call. In the .then() section, the values argument contains the data returned from the various calls. So you can do the stitching that your user champion thought would be the absolute worst.

Go home early, Keeper of Promises. There’s only so much beautiful code to be made in one day!

 

 

 

 

If you have an interest in viewing similar content, visit our blog, here

View our LinkedIn, here

Related posts