Promises in SAPUi5

Promises are useful for async success/failure because you're less interested in the exact time something became available, and more interested in reaching the outcome. 

A promise is an object that may produce a single value some time in the future. It can be one of 3 states:

  • Fulfilled: the asynchronous operation is completed, and the Promise has a value.
  • Rejected: the asynchronous operation is failed. and the Promise will never be fulfilled
  • Pending: the Promise's outcome hasn't yet been determined, because the asynchronous operation hasn't been completed yet.
Promise users can attach callbacks to handle the fulfilled value or the reason for rejection.
When a Promise is pending, it can transition to the fulfilled or rejected state. however, it will never transition to any other state, and its value or failure reason will not change.


Creating Promise:
Promise constructor takes one argument, a callback with two parameters, fnResolve, and fnReject (optional)
Do something within the callback function, perhaps async, then call fnResolve if everything worked, otherwise call fnReject.



Using Promises:
  • The primary way of interacting with a Promise is through its then() method, which registers callbacks to receive either Promise's eventual value or the reason why the Promise cannot be fulfilled.
  • then() takes two arguments, a callback for a success case, and another for a failure case. Both are optional, so you can add a callback for success or failure only.
  • If a Promise has succeeded or failed and you later add a success/failure callback, the correct callback will be called, even though the event took place earlier.


Read Operation using Promise:





Comments

Popular posts from this blog

Exception handing in OData

ALV reort using CL_SALV_TABLE