Synchronous Wrapper Facade in Javascript

D

Daniel Kabs

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. " ([email protected]).

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
 
D

Daniel Kabs

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top