I think no matter what method or mechanism used, or whatever the framework is (Angular/React) that hides it from you, the following principle holds:
In the flow of the program (think code or even the lowest level: machine code), the data may not arrive back 2 seconds later, 3 seconds later, or may not arrive at all, so there is no usual
return
to use in order to return the data.It is the classic "observer pattern". (It can be in the form of a "callback".) It is: "hey, I am interested in knowing a successful arrival of data; would you let me know when it does." So you register an observer to be notified (or a function to be called to notify about the successful arrival of the data.) You also usually register an observer for the failure of arrival of such data.
When it is successful arrival of data, or a failure of the return of such data, the registered observers (or callbacks) are notified together with the data (or called with the data). If the observer is registered in the form of a callback function
foo
, thenfoo(data)
will be called. If the observer is registered in the form of an objectfoo
, then depending on the interface, it could be thatfoo.notify(data)
is called.