Re:Offer HN: I'll answer your javascript questions
by psykotic on Saturday December 18, 2010 @01:53AM
(#2019080)
IMO, a better pattern is to use lexical closures in continuation-passing style to share state:
function sleep(time, continuation) {
setTimeout(continuation, time);
}
function start_process(x) {
var y = foo(x);
sleep(1000, function() {
var z = bar(x, y);
sleep(2000, function() {
baz(x, y, z);
});});
}
start_process(42);
But, to answer your question:
// some code runs here
var continue = function(data){ //more code runs in here after sleep }
setTimeout(function(){ continue(data); },2000);
A different pattern:
var obj = { sleep: function(fn, time){ setTimeout(fn, time ); }, func1: function(data){ console.log('Continuing'); }, init: function(data){
} obj.init()