2. WANT TO HEAR A JAVASCRIPT JOKE?
CALLBACK LATER AND
I'LL TELL IT TO YA!
3. CALLBACKS ARE HOW JAVASCRIPT MANAGES THINGS THAT
NEED TO HAPPEN IN THE FUTURE
fnto dlvrucln( {
ucin eiePnhie)
cnoelg"eas te tk tig ltrly";
osl.o(Bcue hy ae hns ieal.)
}
fnto stpoe){
ucin eUJk(
cnoelg"h dntketmnaslk pn?)
osl.o(Wy o' lpoaic ie us";
stieu(eiePnhie 20)
eTmotdlvrucln, 00;
}
stpoe)
eUJk(;
5. WHAT HAPPENS IF THE EXECUTION THREAD IS BUSY?
fnto dlvrucln( {
ucin eiePnhie)
cnoelg"eas svnhssm bds ttos";
osl.o(Bcue ee a oe aas ato.)
}
fnto cnrvdxml( {
ucin otieEape)
cnoelg"h i sxari o svn";
osl.o(Wy s i fad f ee?)
stieu(eiePnhie 20)
eTmotdlvrucln, 00;
fnPieatrFrETEEYLRENME)
idrmFcoso(XRML_AG_UBR;
}
fnto cnrvdxml(;
ucin otieEape)
14. (The 'X' in 'Ajax' is 'XML', but we'll ignore that)
15. fnto laSuf){
ucin odtf(
vrxr=nwXLtpeus(;
a h
e MHtRqet)
xrorayttcag =fnto( {
h.nedsaehne
ucin)
i(h.edSae==4 {
fxrraytt = )
cnoelgxrrsosTx)
osl.o(h.epneet;
}
}
xroe(GT,'di?ea=';
h.pn'E' /otdly5)
xrsn(;
h.ed)
cnoelg"eus sn!)
osl.o(Rqet et";
}
laSuf)
odtf(;
16. YOU CAN'T WRITE ASYNCHRONOUS CODE SYNCHRONOUSLY
fnto gttf( {
ucin eSuf)
vrxr=nwXLtpeus(;
a h
e MHtRqet)
vrrsls
a eut;
xrorayttcag =fnto( {
h.nedsaehne
ucin)
i(h.edSae==4 {
fxrraytt = )
rsls=xrrsosTx;
eut
h.epneet
}
}
xroe(GT,'di?ea=';
h.pn'E' /otdly5)
xrsn(;
h.ed)
rtr rsls
eun eut;
}
cnoelg"tf i " gttf()
osl.o(Suf s , eSuf);
17. YOU CAN MAKE SOME ASYNCHRONOUS CODE SYNCHRONOUS,
BUT EXECUTION WILL BLOCK ALL EVENTS
fnto gttf( {
ucin eSuf)
vrxr=nwXLtpeus(;
a h
e MHtRqet)
vrrsls
a eut;
xrorayttcag =fnto( {
h.nedsaehne
ucin)
i(h.edSae==4 {
fxrraytt = )
rsls=xrrsosTx;
eut
h.epneet
}
}
xroe(GT,'di?ea=' fle;/ tidprmi te'sn'prm
h.pn'E' /otdly5, as) / hr aa s h ayc aa
xrsn(;
h.ed)
rtr rsls
eun eut;
}
cnoelg"tf i " gttf()
osl.o(Suf s , eSuf);
18. HOW DO WE MAKE THE ASYNCHRONOUS CODE MORE
UNDERSTANDABLE?
CALLBACKS
19. fnto gttf(r,clbc){
ucin eSuful alak
vrxr=nwXLtpeus(;
a h
e MHtRqet)
xrorayttcag =fnto( {
h.nedsaehne
ucin)
i(h.edSae==4 {
fxrraytt = )
clbc(h.epneet;
alakxrrsosTx)
}
}
xroe(GT,ul;
h.pn'E' r)
xrsn(;
h.ed)
}
gttf(/otdly5,fnto(epne {
eSuf'di?ea=' ucinrsos)
cnoelgrsos)
osl.o(epne;
};
)
20. BUT WHAT IF I NEED TO DO THINGS THAT DEPEND ON EACH
OTHER?
30. fnto gttfASaeOjc(r){
ucin eSufshrdbetul
vrxr=nwXLtpeus(;
a h
e MHtRqet)
vrsae ={
a hrd
rsls
eut:
nl
ul
}
;
xrorayttcag =fnto( {
h.nedsaehne
ucin)
i(h.edSae==4 {
fxrraytt = )
sae.eut =xrrsosTx;
hrdrsls
h.epneet
}
}
xroe(GT,ul;
h.pn'E' r)
xrsn(;
h.ed)
rtr sae;
eun hrd
}
fnto cetSaeHnlrsae){
ucin raehrdade(hrd
vrhnlr=fnto( {
a ade
ucin)
i(sae.eut){
f!hrdrsls
stieu(ade,
eTmothnlr
}
es {
le
cnoelgsae.eut)
osl.o(hrdrsls;
}
}
rtr hnlr
eun ade;
}
0;
)
vrsae =gttfASaeOjc(di?ea=';
a hrd
eSufshrdbet'otdly5)
cetSaeHnlrsae))
raehrdade(hrd(;
31. Create an object in a producer that can be told that processing
is done
Get an object from the producer to the consumer so that the
consumer can tell it to do something
32. fnto gttfWtCodntrul {
ucin eSufihoriao(r)
vrxr=nwXLtpeus(;
a h
e MHtRqet)
vrcodntr=nwCodntr)
a oriao
e oriao(;
xrorayttcag =fnto( {
h.nedsaehne
ucin)
i(h.edSae==4 {
fxrraytt = )
codntraloenTeaasxrrsosTx)
oriao.lDnAdhDtI(h.epneet;
}
}
xroe(GT,ul;
h.pn'E' r)
xrsn(;
h.ed)
rtr codntrcnueOjc(;
eun oriao.osmrbet)
}
45. Promises are more than just a fancy callback system. A promise
stands in place of a future value.
46. When an object is "thenable", the value can be retrieved in the
future and used directly, making asynchronous concepts look
synchronous.
47. GOOD THINGS WITH THIS APPROACH
Code is much more flat
Easier to test
We can compose functions that take promises
We can handle exceptions very clearly
We can pass around promises as though they were the actual
value, using .then() to get the result
48. BAD THINGS WITH THIS APPROACH
Tends to cause some confusion because the abstraction isn't
straightforward
Using this in a public API is considered very opinionated, and
forces consumers of your API to use a specific paradigm
Multiple implementations of this, not all meet the standard
(*cough* jQuery *cough*)
50. Asynchronous processes in JavaScript are handled with
callbacks.
Callback mechanisms introduce some problems such as error
handling, composition, testing.
Callbacks are functions, which are first class objects.
Instead of callbacks, what if we used a different object that
mediates between consumer and producer?
Promise objects are a type of mediator that represent the
future value of an async process.
Promises/A+ is an open standard with a specification.
Promise objects can be used in place of actual values to make
asynchronous code look synchronous.