53
Views
1
Comments
Solved
Defining Asynchronous JavaScript Code Question

Hi everyone,

I am studying the Outsystems documentation about Defining Asynchronous JavaScript Code with link:

Defining Asynchronous JavaScript Code - OutSystems 11 Documentation

There's a part that confuses me. I'm not sure what to thin of it. 

So, in the Call an Asynchronous Client Action part, there's a section wherein the documentation states this:

You can also act upon an unhandled error or a call to "$reject()" occurred in the asynchronous client action. To do that, do one of the following:

  1. Add a second argument to the "then()" method. Its full signature is "then(onFullfilled, onRejected)", and the "onRejected" is a callback function called when "$reject()" is called in the asynchronous action, or
  2. Chain the return value of the "then()" method, which is also a promise, with a "catch(onRejected)" method, that will handle the error case – this chaining process is called composition.

It then goes on and gives 2 examples

#Example1

#Example2


So, where my confusion comes in where the documentation says 

You can also act upon an unhandled error or a call to "$reject()" occurred in the asynchronous client action. To do that, do one of the following:

  1. Add a second argument to the "then()" method. Its full signature is "then(onFullfilled, onRejected)", and the "onRejected" is a callback function called when "$reject()" is called in the asynchronous action, or

It talks about the signature "then(onFullfilled, onRejected)" but in the examples above, I do not see any explicit "second" function that caters to the onRejected. I see the catch which I understand, kind of does what is described but what I do not understand is why the documentation talks about "then(onFullfilled, onRejected)", two arguments. But then does not give an example on how it is constructed. Wondering if anyone can give me a sample code snippet on how to construct the "then(onFullfilled, onRejected) " where I can see a function that caters to OnRejected or at least clarify things for me. Thanks!

2024-02-20 19-14-17
Niranjan Kumar
Solution

Hi @Reynald Zackery Saley,

The then() method of Promise instances takes up to two arguments: callback functions for the fulfilled and rejected cases of the Promise. It immediately returns another Promise object, allowing you to chain calls to other promise methods. 

OutSystems documentation page talks about non-chaining approach - full signature is "then(onFullfilled, onRejected)". However, the examples showcased there are using Thening/Chaining approach may be for simplified or commonly used examples.

If you need to understand the Promise implementation (chaining & non-chaining), take a look here.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

- Niranjan


Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.