What are promises?
Promises, as the name says, states for asynchronous mechanisms. The name is pretty convenient if you associate it with the promises in our real world.
When somebody promises you something, then you need to be waiting for the promise to either happen, meaning it was completed as promised or broken in case the person who promised it doesn’t do what he was supposed to do.
That doesn’t mean, in any way, that you need to be waiting and doing nothing. In the meantime, you can keep living your life, and you will know when it gets completed. So this is an asynchronous process.
Here is a very good definition of it from Mozilla:
How to identify promises?
Let’s see what a standard promise looks like:
If I run this code in the console, this is what I get:
So this means the return of the promised has not been filled in yet, but will do as soon it reaches the resolved state. Because it’s an asynchronous mechanism, we need to handle the result after the promise is resolved, so this is how we do it:
When I run this in the console, we can see the result has been handled as expected:
How to use promises in OutSystems?
Here is a simple example of how we could use promises in the platform. In this case, we are creating a promise and making it “wait” until it gets resolved by another button to click.
I added the above piece of code to my OnReady:
The BtnId comes from a button that is on my screen and does nothing, so its click’s action is empty:
So when I publish and test it, this is what happens:
The article’s example demonstrates how Promises can be used to resolve the code only when a button is clicked. Until the Promise is fulfilled or rejected, the code remains idle.
While this is a simple example, it shows the potential of Promises, particularly in front-end development. However, it’s important to keep in mind that overuse of Promises can impact the client’s performance.
In conclusion, Promises are a useful tool for asynchronous programming in front-end development and can improve the user experience without requiring the user to wait for the promise to be resolved. But, as with any tool, they should be used with caution to maintain optimal performance.