Sunday, February 12, 2017

Empty Promises: From Promise to Deferred

The JavaScript Deferred pattern is well documented and used a lot. You've probably even used a jQuery's implementation before.

I needed this "empty promise" pattern for this project and didn't want to be coupled to jQuery anymore than I am by using Bootstrap.

Here's the implementation in TypeScript as an extra layer on Promise:
I have deployed this in various places throughout this project, but one particular example is when a component needs the Selector object (to get selection count, for example). The component initialization is obviously asynchronous. Initializing the Selector object is also done asynchronously and the promise would be better resolved elsewhere. So in the meantime, I would return an "empty promise" to the component requesting the Selector object.