Synchronous Wrapper Facade in Javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Kabs

    #1

    Synchronous Wrapper Facade in Javascript

    Hello,

    loading images or refreshing IFRAME content is done asynchronously in
    Javascript. You just assign a value to the src attribute of the object
    and you are done. Once loading is finished, the event handler will
    inform you. This is wanted behaviour in most situations but sometimes
    its just a pain.

    Imagine, you have this kind of "call" in a deeply nested function. As it
    will return immediately, how do you wait for the result? You can't. You
    have to use callbacks. "It's a little awkward to
    have to string together calling functions and callbacks all over the
    place, particularly when I need to maintain some kind of state between
    those calls. " (chris.farmer@g mail.com).

    Can I wrap something like this

    function ImgObj(obj) {
    var me = this;
    this.load = function() {
    obj.onload = function() { me.loaded()};
    obj.src = "bild.jpg";
    }
    this.loaded = function() {
    alert("Image loaded!");
    }
    }

    with a synchronous facade so that calls to a "load" function return
    either the image object or null in case of a timeout?


    Cheers
    Daniel Kabs
    Germany
  • Daniel Kabs

    #2
    Re: Synchronous Wrapper Facade in Javascript

    Hello,

    I finally figured, it's neither recommendend nor feasible to wrap the
    async calls with a synchronous facade. One should rather look for better
    approaches which lend itself better to the async environment of JS.

    Cheers
    Daniel

    Comment

    Working...