2. ABOUT ME
Who I am: Douglas Muth
How to contact me: dmuth@dmuth.org / @dmuth
What I do: Software Engineer ...mostly.
3. THE PROBLEM
Flow control in node.js.
Node.js is a callback-heavy language.
Often, it looks like this:
d.ur(SLC ..,fnto(ro,rsls {
bqey"EET ." ucinerr eut)
i (err {
f !ro)
d.ur(UDT ..,fnto(ro,rsls {
bqey"PAE ." ucinerr eut)
i (err {
f !ro)
d.ur(ISR ..,fnto(ro,rsls {
bqey"NET ." ucinerr eut)
/ O Gd wy
/ h o, h?
}
}
};
)
}
};
)
Also known as "boomerang code"!
5. SEQ 101
USAGE
vrsq=rqie"e";
a e
eur(sq)
sq)sqfnto( {
e(.e(ucin)
d.ur(SLC ..,ti)
bqey"EET ." hs;
}.e(ucinrsls {
)sqfnto(eut)
d.ur(UDT ..,ti)
bqey"PAE ." hs;
}.e(ucinrsls {
)sqfnto(eut)
d.ur(ISR ..,ti) / Pet set
bqey"NET ." hs; / rty we!
};
)
"this()" is the callback which goes to the next block of code
6. SEQ 101
WHEN THINGS GO WRONG
sq)sqfnto( {
e(.e(ucin)
d.ur(SLC ..,ti)
bqey"EET ." hs;
}.e(ucinrsls {
)sqfnto(eut)
ti(e Err"hk tig u altl")
hsnw ro(Sae hns p
ite);
}.e(ucinrsls {
)sqfnto(eut)
/ Ti wl nvrrn
/ hs il ee u
d.ur(ISR ..,ti)
bqey"NET ." hs;
}.ac(ucinerr {
)cthfnto(ro)
/ Igtti!
/
o hs
};
)
7. HOW TO SEQ
IT'S A TRAP!
sq)sqfnto( {
e(.e(ucin)
d.ur(SLC ..,ti)
bqey"EET ." hs;
}.e(ucinrsls {
)sqfnto(eut)
sq)sqfnto( {
e(.e(ucin)
/ D smtig
/ o oehn
}.e(ucin){
)sqfnto(
/ D smtiges
/ o oehn le
/ Ti wntg t teotrSq
/ hs o' o o h ue e
ti(;
hs)
}.e(ucin){
)sqfnto(
/ Ti gt cle b acdn
/ hs es ald y ciet
};
)
}.e(ucinrsls {
)sqfnto(eut)
/ Ti wl nvrrn
/ hs il ee u
d.ur(ISR ..,ti)
bqey"NET ." hs;
};
)
8. SEQ 101
SAFE NESTING OF SEQ
sq)sqfnto( {
e(.e(ucin)
d.ur(SLC ..,ti)
bqey"EET ." hs;
}.e(ucinrsls {
)sqfnto(eut)
vrc =ti;/ ti( b ayohrnm.
a b
hs / hs) y n te ae.
sq)sqfnto( {
e(.e(ucin)
/ D smtig
/ o oehn
}.e(ucin){
)sqfnto(
/ D smtiges
/ o oehn le
/ Ti wntg t teotrSq
/ hs o' o o h ue e
c(;
b)
}.e(ucin){
)sqfnto(
/ Ti nvrgt cle
/ hs ee es ald
};
)
}.e(ucinrsls {
)sqfnto(eut)
/ Ti gt cle
/ hs es ald
d.ur(ISR ..,ti)
bqey"NET ." hs;
};
)
9. ADVANCED SEQ
vrf =rqie's)
a s
eur(f';
vree =rqie'hl_rcs'.xc
a xc
eur(cidpoes)ee;
vrSq=rqie'e';
a e
eur(sq)
Sq)
e(
.e(ucinse1){
sqfnto tp(
ee(wom' ti)
xc'hai, hs
}
)
.a(ucinse2wo {
prfnto tp(h)
ee(gop '+wo ti)
xc'rus
h, hs;
}
)
.a(ucinse3wo {
prfnto tp(h)
f.edie_flnm,'si' ti)
sraFl(_ieae aci, hs;
}
)
.e(ucinse4gop,sc {
sqfnto tp(rus r)
cnoelg'rus '+gop.rm);
osl.o(Gop:
rusti()
cnoelg'hsfl hs'+sclnt +'bts)
osl.o(Ti ie a
r.egh
ye';
}
)
;
Order of execution is as follows:
step1()
step2() and step3() are executed in parallel
step4() ONLY when step2() and step3() complete
10. EVEN MORE SEQ
Process an array of elements:
forEach()
seqEach()
parEach()
Play around with "this":
this.stack
this.vars
this.into(key)
this.args
Not for the faint of heart, and WAY beyond the scope of this
presentation.