0

Under Inheritance, when a child object extends the parent, the parent needs to be constructed first before the child can be created.

However, one of the properties of parent constructor is async (webSQL database transaction). I want to share the database transaction object to all of it's children.

But then the problem is children can't use the transaction object right away because it gets created some time later asynchronously after the parent creation.

Parent:

    if(this.database = common.model.connections.Sync.getConnection()) {
        this.database.transaction(function(transaction){
            self.transaction = transaction;
        });
    }

Please advise or ask me for more details. Will appreciate any design patterns.

7
  • Not sure if I entirely understand your question, but I think you're looking for Promises: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Sep 3, 2014 at 16:37
  • thanks @NickDugger for the attempt, it's new for me, but won't be able to use since it won't work across browsers, but it seems similar to what I'm thinking. i.e. Pass a callback method (let's say startup) of the child object to the parent object in the constructor, and then parent will call the callback once the async property (database transaction) gets created. Commented Sep 3, 2014 at 16:43
  • There are polyfills for Promises. I highly suggest you give it a try Commented Sep 3, 2014 at 17:42
  • What I understood from my brief research that it's to reduce nesting of callbacks and make the code more organized, but I feel my question is more architecture related, even if you forget for a minute that we are in javascript, let's say a parent's property get's created asynchronously, so basically child is not fully complete right away, it also means the parent has to know the child. Commented Sep 3, 2014 at 20:36
  • Performing database operations in a constructor looks odd to me. Commented Oct 24, 2014 at 19:12

1 Answer 1

0

Async constructors are not supported explicitly. However, there is a straightforward solution: if you can't put the object in the promise, put the promise in the object. In practice this turns out to be a better solution anyway, since you can then check resolution status anywhere you have a reference to the object, not just at the point of construction.

I refer you to my answer to a related question.

async constructor functions in TypeScript?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.