Polling with promises

I'm a big fan of promises in javascript. I think they are a cleaner way to express asynchronous operations than callback. They encourage re-use, are immutable and you don't care when your data is ready. Unfortunately, they are not well understood by the community, and few people are using it.

Here I want to show a quick example to do polling with promises. It came up at work a few days ago. We have to convert video to a given format. To do so, we're using a thirs party which does that. We submit it a job, and then we need to poll its api to see when the job is completed.

Here's the callback based implementation:

Below is the variant using promises. I am using bluebird, a fantastic library for promises, which has a method to transform callback based apis into promise based.

And that's it, Promise.delay really helps to keep things clean.