4. user = getUser(id)
console.log(user.name) # won’t work if getUser() is async
# callback
getUser id, (user) -> console.log(user.name)
# promise
getUser(id).then (user) -> console.log(user.name)
5. Promises/A+
The Promise interface represents a proxy for a
value not necessarily known when the
promise is created. It allows you to associate
handlers to an asynchronous action's eventual
success or failure.
Source: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise
6. Promises/A+ states
pending initial state, not fulfilled or rejected
fulfilled successful operation
rejected failed operation
settled the Promise is either fulfilled or
rejected, but not pending
Source: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise
20. AngularJS $q
$q can be used in two fashions
● Kowal's Q or jQuery's Deferred
● other, similar to ES6 promises
Source: https://docs.angularjs.org/api/ng/service/$q
21. Summary
● promises make code much cleaner and
easier to debug and test
● promises can be:
○ chained (each .than modified result for next .than)
○ executed in parallel ( $q.all(promisesArray) )
○ stored (i.e. for caching)
○ passed as argument to function/object/directive/...
○ returned from function