A callback is essentially a function that is executed after another function has finished executing.
╰ Selection Object
╰ getStart Method
Gets the starting point of selected text. of the Selection object has the following syntax:
It returns a RequestNumberCallback when successfully executed and an ErrorCallback when failed. The call of this method should look like this:
In order to achieve this, the second function should be called in the callback function of the first function:
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.
The calling function must be async in order to wait for Promises:
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.