![](https://technicalgyanguru.com/wp-content/uploads/2024/12/technicalgyanguru.com-36-1.png)
Not only do people maintain their word, but SAPUI5 does too. Every time I hear the term “promise.”
“I’ll come to thee by moonlight, though hell should bar the way. Look for me by moonlight. Watch for me by moonlight.”
One of the strongest promises I have ever read is found in the sentences above. In the process of keeping the pledge, the ardent lover passes away. But fear not—our SAPUI5 Application will survive the need to fulfill its obligation. 🙂
I was inspired to write this promise essay after assisting a friend of mine who was having trouble with standard callbacks. When utilized for asynchronous activities, callbacks can become annoying and complicate the understanding and refactoring of code.
I know some of you are asking what this title Promises means. What is meant by that? What role does it play in SAPUI5?
My friend, hold on. I swear, I’ll go into great length about the Promise idea in UI5. 🙂
To put it simply, promises are similar to pledges you make to people, which you either fulfill or fail to fulfill.
Here are two basic terms.
- Resolved (I fulfilled my pledge)
- Rejected (because I didn’t fulfill my commitment)
Assume you have one straightforward operation to do. This example shows you how to make a promise.
Demo 1: SAPUI5’s Promise of a Simple Single Operation
First, make an example SAPUI5 project.
![](https://sapyard.com/wp-content/uploads/2019/07/image-10.png)
Step 2: As demonstrated below, make a button and write a function.
![](https://sapyard.com/wp-content/uploads/2019/07/image-11.png)
![](https://sapyard.com/wp-content/uploads/2019/07/image-12.png)
The variable test was hardcoded as true, so that it would print “Hey, I was resolved” in the resolved case.
![](https://sapyard.com/wp-content/uploads/2019/07/image-13.png)
Let’s now modify the test to boolean false in order to reject the promise.
![](https://sapyard.com/wp-content/uploads/2019/07/image-15.png)
![](https://sapyard.com/wp-content/uploads/2019/07/image-17.png)
Suppose that the error function returns a message in the event that an error occurs. Additionally, I’ve included some remarks for your convenience.
![](https://sapyard.com/wp-content/uploads/2019/07/image-18.png)
![](https://sapyard.com/wp-content/uploads/2019/07/image-19.png)
I hope you now understand the single operation. Let’s now experiment with three asynchronous real-time executions.
Demo 2 – Multiple Promises in SAPUI5
Step3:- Create a 3 promises as shown below
![](https://sapyard.com/wp-content/uploads/2019/07/image-20.png)
![](https://sapyard.com/wp-content/uploads/2019/07/image-23.png)
After all three are taken care of, you may now need to carry out some business operations. We employ the promise to manage this asynchronous action.every one works.
Managing the Promise.all work.
Put differently, the Promise.all UI function helps to initiate a lot of asynchronous calls. Once all of the calls are finished, we may write logic in accordance with the requirements of the business and carry out the activity.
![](https://sapyard.com/wp-content/uploads/2019/07/image-24.png)
![](https://sapyard.com/wp-content/uploads/2019/07/image-25.png)
Let’s now say that a function encountered an error. Recall that just one error result is shown in accordance with the new use case. The remaining errors, if any, will be disregarded.
![](https://sapyard.com/wp-content/uploads/2019/07/image-26.png)
![](https://sapyard.com/wp-content/uploads/2019/07/image-27.png)
Use Promise to create the mixture of the rejected and resolved responses.allAs indicated below, settled. First one resolved and the others rejected according to the new use case.
Managing the Promise.function allSettled.
![](https://sapyard.com/wp-content/uploads/2019/07/image-29.png)
![](https://sapyard.com/wp-content/uploads/2019/07/image-30.png)
Demo 3: Managing the Initial Reaction from Numerous Promises
Step 4: The winner is the first answer.
You have three asynchronous calls or requests, and it is your business requirement to execute the action in accordance with the outcome of the first call.It’s similar to a rat race in which the first person to solve the problem the fastest wins.
Promise 1 finishes in 200 milliseconds, Promise 2 rejects in 100 milliseconds, and Promise 3 rejects instantly. Promise 3 is the race winner, then! Bravo. Let’s examine the programming for it.
![](https://sapyard.com/wp-content/uploads/2019/07/image-31.png)
The Promise.race method is handled.
![](https://sapyard.com/wp-content/uploads/2019/07/image-32.png)
![](https://sapyard.com/wp-content/uploads/2019/07/image-33.png)
Synopsis
An object called a promise indicates whether an asynchronous operation is successful or unsuccessful. In other words, rather than passing callbacks into a function, a Promise is a returned object to which we attach callbacks.
How Would You Make a Promise?
var promise = new Promise(function(resolve, reject) {
// do a something, possibly async, then…
if (/* everything turned out successful */) {
resolve("Passed!");
}
else {
reject(Error("Failed"));
}
});
One input and a callback with two parameters—resolve and reject—are given to the Promise constructor.
How should I use the Promise produced above?
promise.then(function(result) {
console.log(result); // "Passed!"
}, function(err) {
console.log(err); // "Failed"
});
The callback for the happy path and the callback for the failure path are the two arguments that then() takes.Both are optional, and if we’d like, we can just include the success or failure.
Promises can be used for chaining.
Then() can be used in a chain to carry out numerous additional functions and tests. When the prior operation is successful, we can begin a new one.
Promise API References
Builder
![](https://sapyard.com/wp-content/uploads/2019/08/Constructor-in-Promises-API-in-SAPUI5.jpg)
Static Methods
![](https://sapyard.com/wp-content/uploads/2019/08/Promises-API-in-SAPUI5.jpg)
Instance Methods
![](https://sapyard.com/wp-content/uploads/2019/08/Instand-methods-in-Promises-API-in-SAPUI5.jpg)
A novel idea for ABAP developers studying UI5 is promises. It has many benefits. Please ask questions in the space provided below if you are having any problems or are having trouble putting it into practice.
I swear to answer all of your questions.
you may be interested in this blog here
Salesforce Roadmap to Success: Mapping Out Your Training Journey
Empowering Your ABAP ALV with Integrated Data Access (IDA) on HANA:…