skip to Main Content

Dev Tip: Stitching together disparate OData calls using Promises.

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

Paul Modderman loves creating things and sharing them. He has spoken at SAP TechEd, multiple ASUG regional events, ASUG Fall Focus, Google DevFest MN, Google ISV Days, and several webinars and SAP community gatherings. Paul's writing has been featured in SAP Professional Journal, on the SAPinsider blog, and the popular Mindset blog. He believes clear communication is just as important as code, but also has serious developer chops. His tech career has spanned web applications with technologies like .NET, Java, Python, and React to SAP soutions in ABAP, OData and SAPUI5. His work integrating Google, Fiori, and Android was featured at SAP SAPPHIRE. Paul was principal technical architect on Mindset's certified solutions CloudSimple and Analytics for BW. He's an SAP Developer Hero, honored in 2017. Paul is the author of two books: Mindset Perspectives: SAP Development Tips, Tricks, and Projects, and SAPUI5 and SAP Fiori: The Psychology of UX Design. His passion for innovative application architecture and tech evangelism shines through in everything he does.

Back To Top