Besides collections and objects for most document elements, the new JavaScript API implements asynchronous methods with callback functions. As all methods are asynchronous, it is important to wait for the return value which is solved using callbacks.

A callback is essentially a function that is executed after another function has finished executing.

The JavaScript method getStart TX Text Control .NET Server for ASP.NET
JavaScript API
Selection Object
getStart Method
Gets the starting point of selected text.
of the Selection object has the following syntax:

Loading...

It returns a RequestNumberCallback when successfully executed and an ErrorCallback when failed. The call of this method should look like this:

Loading...

The above code returns safely the selection start index of the current input position. Now, what if we need to check the selection length value in the same function? JavaScript is an event driven language. That implies that Javascript is not waiting for a response before moving on. In the following function, we cannot be 100% certain that the value of the first function is returned first.

Loading...

In order to achieve this, the second function should be called in the callback function of the first function:

Loading...

In case, you need the return values of these two functions in the function itself to do something else, you would have to create a lot of nested functions. That would result in complex structures which makes it the hard to read the code.

Therefore, Promises and async functions can be used. The following function returns a range object with the start and end index of the current selection. The return value itself is a Promise that can be resolved and rejected.

Loading...

The calling function must be async in order to wait for Promises:

Loading...

The keyword await marks the function to wait for a returned Promise, so that the return value can be used in further code in the same function.