How to do asynchronous operation in object constructor
How to do object-oriented programming (OOP) with asynchronous calls? An object constructor is responsible for initializing the state of the object, but async calls don't fit the equation well. How to approach this?
"How to do an asynchronous operation in a constructor?"
Let's approach this problem with the tried and true principle: separation of concerns .
Constructor sets only variables
The constructor function is best left to set internal state at the variable level without making any calls, be it synchronous or asynchronous calls. This is destined by the mechanics of constructors - the newly created object is returned as the result of a constructor function. This rules out returning a Promise as a return value. One alternative would be to accept a callback parameter, but it would lead to quite unorthodox code. You'd have to assign the result of a constructor and then refer to itself inside the callback given to itself. Not a very elegant solution. It's best to leave constructor for setting variables only and use separate initializer method to perform asynchronous duties.
Dedicated initialize() method for async
A separate initializer method (such as
) can be used to set up the object state. It would then use as many asynchronous calls as necessary to get the object properly set up. This way there is a separation of setting up variable values in the constructor and interacting with external parts in the initializer method.
The responsibility of calling the initializer method can be thought two ways.
- You can leave it for the user of the object. You can require that the initializer is called before any operation or otherwise the behavior is undefined.
- The other alternative is to keep the initializer method not part of the public API. Instead, you call it under the hood at the beginning of each public method. In this case, it's a good idea to add guard logic to only perform anything on the first call and let it pass through on subsequent calls.
An example with web resources
Let's create an example that utilizes an initializer method. We'll create a class called
WebResource that represents a URL and its contents. The constructor accepts only one argument: the URL of the resource. Two other methods are provided to get the contents and calculate a hash of the contents.
Here we've chosen to use the hidden initializer approach. Every public method of the API begins with a call to the internal initializer method. It'll only do work on the first call so it won't be overhead.
Bluebird's coroutines are used to ease the pain of dealing with multiple asynchronous operations.
You can find this code and a TypeScript version over at GitHub repo.
Separate the two - construction and async initializing
It's cumbersome to make a constructor perform asynchronous calls. It's best to leave constructor set up the internal state of the object at the variable level without making any calls. A dedicated initializer method can be used to make asynchronous calls and finish setting up the object state. The method can either be called by the user of the object or internally at the beginning of each public method.