Jonas Myrlund

Developer at @appear_in.


Implementing promises. Ish.

Promises can be hard to get a grip on. While I’ve used them quite a bit myself, I’ve never stopped to consider how to implement them – that is, until now.

Disclaimer: To be completely honest, the main goal of this article is for me to properly teach myself “the magic” behind promises. You are, however, welcome to come along for the ride!

I’ll be implementing a subset of the A+ promise spec, that is, enough to have the following run as expected:

Promise.resolve("Never gonna give you up")
  .then(console.log)
  .then(() => { throw new Error("Never gonna let you down") })
  .then(() => console.log("Never gonna print this sentence"))
  .catch(error => console.log(error.message))
  .then(() => {
    console.log("Never gonna run around");
    return Promise.reject(new Error("And desert you"));
  })
  .catch(error => console.log(error.message));

You’ll never guess what the output will be.

...

Continue reading →