Q promises

http://joseoncode.com/2013/05/23/promises-a-plus/

by Denise Nepraunig

HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/q.js/0.9.2/q.min.js"></script>

JavaScript

/*
 * simulate an ajax operation to fetch a customer profile
 */
function getUser (id) {
  return Q.delay(1000)
  .thenResolve({
      id: id, 
      name: 'user ' + id, 
      twiterHandler: 'jfroma'
  });
}

/*
 * simulate an asynchronous fetch from twitter.com
 */
function getTweets (customer) { 
  return Q.delay(1000)
  .then(function() {
    return [{message: 'I love Q and Promises/A+'}, {message: 'I think I am getting used to it'}];
  });
}

getUser(123)
  .then(getTweets)
  .then(function (tweets) {
    tweets.forEach(function(t){
      alert(t.message);
    });
  });