Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I had a great idea for a new javascript keyword "delay", but I don't know what I can do to bring it to the new specification. Also I want to know what you guys think about it and if it's even realistic.

What does the delay keyword ?

The delay keyword does nothing more than stop the execution of the current stack and immediately continues to the next "job" in the queue. But that's not all! Instead of discarding the stack, it adds it to the end of the queue. After all "jobs" before it are done, the stack continues to execute.

What is it good for?

delay could help make blocking code non-blocking while it still looks like synchronous code. A short example:

setTimeout(function(){
    console.log("two");
},0);
console.log("one");
delay; //since there is currently another task in the queue, do this task first before continuing
console.log("three");

//Outputs: one, two, three

This simple keyword would allow us to create a synchronous-looking code wich is asynchronous behind the scenes. Using node.js modules, for example, would no longer be impossible to use in the browser without trickery.

There would be so many possibilites with such a keyword!

Is this pattern useful? What can I do to bring this into the new ECMAscript specification?

Note: I asked this previously on Stack Overflow, where it was closed.

share|improve this question
3  
The capslock usage in parts of your question is quite annoying. –  ThiefMaster Jul 4 '12 at 16:39
    
@ThiefMaster I'm sorry, would it be better to make it bold? –  Van Coding Jul 4 '12 at 16:40
    
Why emphasize it at all? The part about closing doesn't even belong into the question. Anyway, what you suggest sounds like you want something similar to the await keyword of C#. There's already something that implements such as behaviour in a preprocessor style: TameJS (I would link to tamejs.org but it's a 403 for some reason) –  ThiefMaster Jul 4 '12 at 16:41
    
How is this different/better than jQuery.Deferred() ? –  Steve Evers Jul 4 '12 at 18:42
add comment

4 Answers 4

It's funny that your question has been downvoted, since the feature you have described is actually the object of an official strawman1 proposal (but the actual name of the new keyword is await).

If you want to support this proposal, you can suscribe to the es-discuss mailing-list and look for the previous discussions about it.

1 Strawman is the namespace where are keeped the ES6 proposals before validation. The other one is Harmony, where are keeped the ES6 proposals which have been approved but need more work.

share|improve this answer
    
Knowing what a strawman argument is, I don't see how the OP's question is one? Are you saying there is a keyword to be accepted into the standard to do precisely this already? If so I would say the OP just isn't aware of it perhaps, there's no value in him making a strawman proposal here... –  Jimmy Hoffa Nov 22 '12 at 3:54
1  
@JimmyHoffa I agree, I have answered this question to say that there is already a proposal for this feature, and so that the OP doesn't have to make another proposal (he can support the first by posting on the es-discuss mailing-list). I have edited the answer, maybe I wasn't clear enough (english isn't my native language, so ..). –  Maël Nison Nov 22 '12 at 4:02
    
Ah, I see you didn't mean insult, this is great information then, +1. I would remove the word strawman if I were you, I think it might be someone's pseudonym? Either way, in english strawman has a negative connotation (it refers to a propaganda/deception technique) –  Jimmy Hoffa Nov 22 '12 at 4:08
    
Oh, ok ! Didn't know this, thanks :) I have add a footnote to explain the context, I think it should prevent more confusion –  Maël Nison Nov 22 '12 at 4:25
    
Interesting... I've also participated on the es-discuss mailing-list after I asked the question here and got downvoted. On the mailing list I also got heavy resistance against such a feature and they said it's impossible. If it really would hit the spec, this would be great! –  Van Coding Nov 22 '12 at 7:15
add comment

Just a personal opinion, but look at either:

  • barebone setTimeout() and setInterval(),
  • or a bunch of libraries like Dojo's defer().

I don't see where the point would be to make this a language keyword. This seems overboard, when there's already sufficient tools in the standard library and higher-level enhancements in 3rd party libraries.

share|improve this answer
    
It seems you misunderstood the functionality of 'delay'. There is currently no way to do what delay would be able to do. –  Van Coding Jul 5 '12 at 7:45
    
@VanCoding: so, what you want is the equivalent of a classic fork() -> join() paradigm? –  haylem Jul 5 '12 at 8:35
    
not really, no. I don't want to create a new thread or process or something like that. delay would just allow to write code in a synchronous way while in the background it is asynchronous. You wouldn't have to heavily change your code to make it non-blocking. With defer or setTimeout this IS the case! delay just means "hey, other things need to be done before I can continue, so put me at the end of the queue" –  Van Coding Jul 5 '12 at 9:17
    
@VanCoding That's exactly what setTimeout(function(){}, 0) does - it bumps the function passed in to the end of the queue. Moving the remainder of your code (the console.log("three"); in the question) isn't a big deal –  Izkata Jun 3 '13 at 16:46
add comment

To me, it sounds like an irritatingly gimped version of coroutines. Having the ability to yield to another coroutine explicitly would be far more useful than a general “yield to something, I don't know what” which is what defer is.

share|improve this answer
add comment

In my understanding,

 window.setTimeout(function() { console.log("three"); }, 0);

does about the same thing, so there is no need for this keyword, at least not in the browser.

share|improve this answer
    
Yes, but you didn't get the point. With the "delay" keyword, you can program in a synchronous way without blocking the whole program. This keyword was especially meant for the browser. There are a lot of node.js modules out there, that use the synchronous module system of it. Since we can't do synchronous calls without blocking at the moment, it's impossible to use node.js modules in the browser without trickery. I know there are solutions for this, tough. I just wanted to give you an idea of how it could be used ;) –  Van Coding Jul 4 '12 at 16:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.