Broken Promises

One day soon, you will feel like an idiot. Everyone tells you that in this industry. I used to feign believing them purely as a symptom my very fake humility. Now I fully believe them purely as a symptom of my very real humiliation.

You see, as a top tier imposter, I've become a sort of localized beginner expert (label credit: James Gibson of Flowhub). In my little fish tank as a code school student with Galvanize I've become pretty sure of myself.

And then, promises.

For those who may be unfamiliar, Promises are something Satan invented to make sure he waited to torture you until after he stole your soul.

He needed a way in his favorite programming language, Javascript, of course, to reliably automate tasks that had unknown time requirements and had to be done in a particular sequence.

When you need to automate heating up an oven for baking, say, the souls of the damned, but you don't know how long it's going to take to steal those souls, what's an imp to do?

As we all know, stealing souls happens asynchronously, and Satan was wasting a lot of brimstone brewing up an inferno before the souls even showed up.

With Promises, he was able to make sure not to flip on the lake of fire until after little Sammy Shoplifter drew his last breath and landed safely in eternal damnation. He accomplished this like so:

  <script>
    let SammyShoplifter = {
      soul: 'Hangs in the balance'
    };

    //a dormant lake of fire needn't any brimstone.
    let brimstone = 0;

    //how do you bring that lake to a nice simmer? add brimstone... duh.
    let fire = () => {
      brimstone++;
      console.log(brimstone)
    }

    //define Satan's important work of stealing souls
    let steal = waywardSoul => {

      //I don't know when or if this wayward soul is going to be in the bag.
      //If he stays evil, I deliver on my promise, aka "resolve"
      //if he turns to good, it fails, aka "reject".

      return new Promise((resolve, reject) => {

          //souls can go either way. flip a coin
          waywardSoul = Math.random() >= 0.5 ? 'evil' : 'good';

          if(waywardSoul == 'evil') {

            //send this punk to hell!
            resolve(waywardSoul);
          } else {

            //deathbed conversion
            //notice I don't specifically put reject in here. I don't need to,
            //Any thrown error will trigger a reject.
            throw TypeError('Soul says: I\'ve seen the light!')
          }
      })
    }

    //enough theory. The bell tolls for you, Sammy Shoplifter, aka call function
    steal(SammyShoplifter.soul)

    //When I finally win this soul get the fire started like so
    .then(judgement => {
      console.log(judgement)
      fire();
    })
    //but if he listens to his better angels, catch that error
    .catch(repentance => {
      console.log(repentance)
    })

    //either way, I'm getting Torchy's Tacos after this
    .finally(() => {
        getTorchys(trailerParkExtraTrashy)
    })
  </script>

Promises really have been quite useful for our favorite fallen angel. But this doesn't tell the half of it. The great thing about this pattern is that you can throw as many .then() clauses in the chain as your faithful heart desires

Even better, you can still leave the one .catch() and it will handle whatever error you throw at it from any and all .then() blocks.

Here's where the humiliation comes in.

stalk me here