Events + Promises

In notebook:
FrontEndMasters Async Javascript
Created at:
2016-09-21
Updated:
2016-09-21
Tags:
Fundamentals JavaScript
Concurrency: Events + Promises

Managing the flow control. Promises can be resolved once. What if, if we have a stream of events?

Wire promises into an event stream? Most programming is event oriented. 

For example wiring a button click:
  var p1 = new Promise(function(resolve, reject){
  $("$btn").click(function(evt){
    ...
    if(...) {
      resolve();
    } else {
      reject():
    }
  })
})

p1.then(function(className){
  console.log(className);
})
Of course it won't work. It can only be resolved once. 

So you might want to revert the logic and put the promise creation inside the ​click​ event
  $("#btn").click(function(evt){
  ...
  var p1 = new Promise(function(resolve, reject){
    if(...) {resolve();}
    else {reject();}
  })
  p1.then(function(className){
    console.log(className);
  })
})
For each event a new promise. But then, why? For each event, a new promise is created and immediately resolved
Completely useless

The two responsibilities here (source and subscription) are totally mixed cannot be nicely separated. It's inverted. The source and subscription is set up in the same spot.
Real anti-pattern.