the problem begin when you have to force a synchronous order to a sequence of asynchronous command.
the mechanism of callbacks quickly turned for me to a chain of ugly callbacks that made my code much harder to read.
for example:
Code: Select all
//the callbacks technique:
function foo(){
doSomethingAsnyc(value, callback);
}
callback(bar){
doNextAsyncCommand(bar, anotherCallback);
}
anotherCallback(bar2){
//and so on...
}
this technique actually breaks down the whole "tree like" structure of the program and forces me to break a logical high level function into a few little functions. its like turning the tree into a pile of leaves.
what i would like to do is to be able to write something like this:
Code: Select all
function foo(){
bar=doSomethingAsync(value);//wait without blocking;
doNextAsyncCommand(bar);//wait; without blocking;
//and so on...
}
this is how things look like in most other languages, but as we already know, in JavaScript it is more problematic because JavaScript is a single threaded language.
there are some smart way to work around things like using a "state machine like" mechanism and promises...but none of them is simple as just waiting for the asynchronous call to return.
and now for my question:
why not using the already exist mechanism of closure to introduce a new none-blocking "wait" feature into javascript??
what i mean is that if you'll think about it, all we need is to insert a new syntax that will cause the javascript interpreter to use the next line as an anonymous handler for the current asynchronous call, just like "function" label tells the interpreter to consider the next line as the body of a function (not exactly, but you know what i mean). like this:
Code: Select all
function foo(){
//wait is a new syntax proposal that will tell the interpreter to wait for the //asynchronous call to finish. behind the scenes "wait" could be implemented by //telling the interpreter to use the next line as the handler of this call,
//and use the function scope as the closure to this "fake" handler.
//we also need to save tell the interpreter to save the current stack.
wait bar=doSomethingAsnyc(value);
doNextAsyncCommend(bar);
}
another example:
Code: Select all
function foo(){
var result = "error";
var key=getKey();
if(verify(key)){
var name=getName();
if (verify(name)) {
login(key,name);
result = "ok";
}
}
return(result);
}
now lets say that getKey and getName both need to wait to an event to happen so they asking for a callback.
and let us say that getName is meaningless without getting the key first...so we need to put it in the callback to wait.
this will force us to break the function and chain the callbacks like this:
Code: Select all
function foo(){
getKey(callback);
}
function callback(key){
if verify(key){
getName(callback2);
}
function callback2(name){
if verify(name){
return "ok";
}
}
but this is ugly!
why couldn't we just do something like this:
Code: Select all
function foo(){
var result = "error";
wait var key=getKey();
if(verify(key)){
wait var name=getName();
if (verify(name)) {
login(key,name);
result = "ok";
}
}
return(result);
}
now lets imagine that behind the scenes the wait syntax is doing exactly the same as the chained callbacks...
isn't it great?
so my questions:
1. what do you think about this proposal?
2. is it possible to implement it using only existing mechanism of the current js interpreter features?
let my clarify just one thing:
I'm not suggesting to make JS a multi-threaded language or something fancy..actually i don't want to change anything in the inner-working of JS. I'm trying to suggest a syntactic sugar only and i want to understand if it is possible to introduce it using only existing mechanisms.
do someone here actually knows well how the interpreter of FireFox handles the stack and if it is possible to introduce this kind of syntax exploiting already existing mechanisms?
i can think of few way to implement this with closures like mechanism but i need a better understanding of the stack handling first...
so what do you think???
update:
after posting the same question on another site, things where quickly escalated to a H-U-G-E misunderstanding of my intentions, so let me give another last clarification:
Right now, if i wanted so, i could easily build a "compiler", or more exactly a pre-processor, that would run over my code and would turn it from this:
Code: Select all
function foo(){
var result = "error";
wait var key=getKey();
if(verify(key)){
wait var name=getName();
if (verify(name)) {
login(key,name);
result = "ok";
}
}
return(result);
}
into this:
Code: Select all
function foo(){
getKey(callback);
}
function callback(key){
if verify(key){
getName(callback2);
}
function callback2(name){
if verify(name){
return "ok";
}
}
now all i am saying is that instead of using external pre-processor, why not just teaching the FireFox interpreter to do it on its own???
hope that my intention are clearer now;)