PROMISES IN
JAVASCRIPT
ABOUT ME
Rubyist / JavaScripter
Yeoman team member
http://blog.revathskumar.com
Twitter/Github - @revathskumar
WHAT ARE PROMISES?
A pattern to work with asyncronous operations much neatly.
A function return a object called promise, which is the eventual
result of the operation.
operated via then method.
States : Pending / Fulfilled / Rejected
poiete(nufle,oRjce)
rms.hnoFlild neetd
WHY WE NEED PROMISES?
Chaining
Attach multiple
callbacks
Pool operations
Easy error handling
Ue.e(,
srgt1{
sces fnto({
ucs: ucin)
cnoelg'ucs';
osl.o(Sces)
}
,
err fnto({
ro: ucin)
cnoelg'ro';
osl.o(Err)
}
}
)
IN PROMISE
Ue.e()te(
srgt1.hn
fnto({
ucin)
cnoelg'ucs';
osl.o(Sces)
}
,
fnto({
ucin)
cnoelg'ro';
osl.o(Err)
}
)
;
Ue.e(1,{
srgt''
sces fnto({
ucs: ucin)
Acut.e(1,{
consgt''
sces fnto({
ucs: ucin)
Acut.pae{wte:@dysai,{
consudt(titr adomn}
sces fcto({
ucs: unin)
cnoelg'ucs'
osl.o(Sces)
}
,
err fnto({
ro: ucin)
cnoelg'ro';
osl.o(Err)
}
}
)
}
,
err fcto({
ro: unin)
cnoelg'ro';
osl.o(Err)
}
}
)
}
,
err fnto({
ro: ucin)
cnoelg'ro';
osl.o(err)
}
};
)
Ue.e()
srgt1
.hnAcut.e)
te(consgt
.hnfnto({
te(ucin)
rtr Acut.pae{wte:@dysai)
eun consudt(titr adomn}
}
)
.hnfnto({
te(ucin)
cnoelg'ucs';
osl.o(Sces)
}
)
.hnudfnd fnto({
te(neie, ucin)
cnoelg'ro';
osl.o(Err)
};
)
vrUe =(ucin)
a sr
fnto({
fnto gtue_d{
ucin e(sri)
vrpoie=nwAls)
a rms
e pu(;
$gt'ues'+ue_d {
.e(/sr/
sri,
sces fnto(aa{
ucs: ucindt)
poieflildt.con_d;
rms.ufl(aaacuti)
}
,
err fnto({
ro: ucin)
poierjc(Err)
rms.eet'ro';
}
};
)
rtr poie
eun rms;
}
rtr {
eun
gt gt
e: e
}
}
)
POOLING
Alspo(
pu.ol
aycuc)
snFn(,
aohrsnFn(
nteAycuc)
)
.hnaycucs,aycro)
te(snSces snErr;
USING JQUERY
$we(
.hn
$aa("pg1pp )
.jx /ae.h" ,
$aa("pg2pp )
.jx /ae.h" ,
)
.hnoSces oErr;
te(nucs, nro)
JQUERY : MULTIPLE CALLBACKS
$we(ucin)
.hnfnto({
cnoelg'el Poie)
osl.o(Hlo rms';
}
)
.hnfnto({
te(ucin)
cnoelg'ucs1)
osl.o(Sces';
} fnto({
, ucin)
cnoelg'ro1)
osl.o(Err';
}
)
.hnfnto({
te(ucin)
cnoelg'ucs2)
osl.o(Sces';
} fnto({
, ucin)
cnoelg'ro2)
osl.o(Err';
};
)
REFERENCE
1.
2.
3.
4.

http://promises-aplus.github.io/promises-spec/
http://wiki.commonjs.org/wiki/Promises/A
https://github.com/rhysbrettbowen/Aplus/
http://api.jquery.com/category/deferred-object/

Promises in JavaScript