Callbacks
A callback is that you register a piece of code (almost always a function) that will run when an event occurs.
1 |
|
This is an example of registering a callback. In this case onclick="myFunction()"
is registering a callback with the function myFunction()
Promises
A promise is an Object that will call you back when it has something to give you. You need to register a callback that will be called when the Promise is ready to deliver its goods.
When Promise is done doing its thing (fetching data, counting…), it calles the callback function that was defined.
When we are using the Promise, actually we are defining argument to then()
. When we define callback functions for a Promise, we can define up to 3 functions, one called when the Promise is resolved, one called when the Promise is rejected, and another one when the Promise has error.
Use Promise
Ionic Storage returns a Promise when use .get(key)
.
1 | let myData: any; |
Promise.then()
is doing two things when you call it:
- Registering a callback
- Signaling to the Promise that it should start doing its work
- Consumer calls
Promise.then()
=> Promise run function defined in itself => call resolve() if function success => Run callback functions defined in.then()
, the callback should take one parameter: the result
1 | // Promise Provider |
1 | // Promise Consumer |
Good to Know
- Most Promises are used when a function call could take a long time.
- How to use Promise as Consumer is more important
- Async function always returns a Promise with resolved value of return value.
Observables
Difference between Promise and Observables:
- A Promise does its work only once. When the consumer calls the
.then()
, the Promise’s would execute and done - An Observable keeps working. It returns a stream of results, rather than just a single result. A consumer, instead of calling
.then()
, calls.subscribe()
. The Consumer defines a callback function that will run whenever the Observable has results to share. - The callback function will run whenever the Observable is updated
.map()
TBA
Creating Observable
A good use case for Observables is anytime that you have some data that could be updated by different part of your app and you want to be able to notify the other parts of the app that the data has changed.
Observer
Observer is the piece of code that observes some data and makes that data observable by notifying subscribers of changes. The Observer provides Observables to subscribers, and uses those Observables as the communication channel by which to notify them of changes.
1 |
|
At Runtime:
- The Consumer calls a function (e.g.
getObservable()
) that returns an Observable - Inside that function, the
Provider
creates the Observable (Observable.create()
), and in the process obtains anObserver
that is linked to theObservable
that is returned. This linkage happens inside theObservable.create()
. - Once the Consumer has a handle to the Observable, it calls
subscribe(callbackFunction)
.
1 | obs = HeroesService.getObservable(); |
- At this point nothing actually happens. All that has happened is the Consumer has registered itself with the Observable as “interested”, but the Consumer’s callback function won’t actually get called until the Observable has a result.
- When the Observer has a result, it calls
myObserver.next(result)
. It automatically “notifies” all of the Consumers who have subscribed to the linked Observable of the result by calling the callback function that was registered when they registered. - As the Observer has more result, the Consumer’s callback function is called again with the new result.
AJAX and APIs
AJAX (Asynchronous Javascript And Xml)
crossorigin.me to solve CORS problem
1 | fetch('URL/api/') |