Promises in JavaScript

Boopathi Rajaa
May 29, 2015 · 3 min read
a('a', function() {
b('b', function() {
c('c', function() {
d('d', function() {
console.log('WTF!!!');
});
});
});
});

The analogy

var foo= 5;
var bar = {};
var baz = 'dice';
var promise = asyncfn();

A Stateful Machine

First class citizen

var x = 1;
var y = x + 5;
var x = delay1s(1);
var y = x.then(function(val) { return val + 5; });

Single output pattern

Concurrent usage pattern

The Mutate anti-pattern

var promise = delay(100).then(_ => ({ a: 5 }))
var x = promise.then( v => { v.a = 6; return v })
var y = promise.then( v => { console.log(v); return v })

Epilogue

Boopathi Rajaa

Written by

A web developer with unhealthy interest in JavaScript and Go

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade