1 of 23

Slide Notes

DownloadGo Live

Angular JS Promise

Published on Nov 18, 2015

Almost done

PRESENTATION OUTLINE

Angular JS PROMISE

CHAINING, EXCEPTION HANDLING, & ANTI-PATTERNS
Photo by ditatompel

Jimmy Chandra

T: @jchandra E: jimmy.chandra@coupa.com
Intro to self here,
perhaps a bit of an icebreaker as well.. joke, whatever.
Photo by ginnerobot

Promise?

What's That?
What's a promise?
(Not the one in background)
What's so good promise?
Why should we use it?

Synchronous

IMMigration Line, Supermarket Checkout, etc.
Real life example...
Waiting in line in bank, grocery store, immigration line.

Why is this not good?

Perception: slow.

From the clerk's / teller / custom officer perspective...
Serving one client at a time from start to finish before can move on.
Photo by illustir

ASYNCHRONOUS

ReSTAURANT, PHARMACY, ETC.
Like to eat out?
Restaurant?

Let's discuss the waiter...

From waiter's perspective...

Take order,
bring order to kitchen...
then what? wait till it's done?

No, go take another order from different table, etc.

Only and only when food is ready (bell - food ready), they go back to kitchen and take the meal to the client.

Why is this better?
Perceptions: faster.

From resto owner perspective? Efficient use of limited resource.

So async is good.

So how does this tie to Angular?
Anyone use promise before in angular?
Get example...
get them to think $http or $resource...
flip to next slide...

promise returning angular services

  • $q
  • $http
  • $timeout
Things I know in Angular that return promise...

talk a bit about $timeout and how to use it for mocking server side call and why $http can't do it w/ out server...
Photo by Andrey Zhukov

var foo = $http.get('/data.json');

Trap...

ask what will foo have...
data.json content?

explain people are using this incorrectly.

talk about how it was actually correct in the old day of angular.

Demo: demo01
Photo by Stéfan

DEMO

Photo by hackNY

PROMISE CHAINING

SERIAL CHAIN

waiter
.takeCustOrder()
.then(handOderToKitchen)
.then(takeOrderToTable)
.catch(tellProblemToCust);

Photo by pratanti

PARALLEL CHAIN

$q.all([prepareSauce(), cookPasta(), prepareSalad()])
.then(notifyOrderReady)
.catch(unableToFulfillOrder);

Photo by Dusty J

AUTO-RESOLVING PROMISE
return $q.when(value);

Photo by keightdee

DEMO

Photo by hackNY

Exception HANDLING

Photo by micheleorsi

SERVICE EXCEPTION HANDLING
.catch(handleProblem);
function handleProblem(resp) {
logger.logError(resp);
return $q.reject('unable to ...');
}

UI EXCEPTION HANDLING
.catch(handleProblem);
function handleProblem(reason) {
alert('Problem, contact support');
}

DEMO

Photo by hackNY

ANTI-PATTERN

var dfd = $q.defer();
$timeout(function() {
return dfd.resolve(0);
}, 0);
return dfd.promise;

DO THIS INSTEAD

return $timeout(function() {
return 0;
});

Photo by DieselDemon

ANTI-PATTERN

someService
.doSomething()
.then(funtion(res) {...}, function(res) { ... });

DO THIS INSTEAD

someService
.doSomething()
.then(successHandler)
.catch(failHandler);

Photo by DieselDemon

IN THE NEAR FUTURE

- ES6 Promise
- Observables

Photo by Clauz Jardim