I'm developing a console like script for personal needs... I need to be able to pause for a extended amount of time, but as node.js from my research has no way to stop as required.... Its getting hard to read users information off after a period of time... iv seen some code out there, but i believe they have to have other codes inside of them for them to work such as:
setTimeout(function() {
}, 3000);
But this issue is, i need everything after this line of code to execute after the period of time...
For example,
//start-of-code
console.log('Welcome to My Console,');
some-wait-code-here-for-ten-seconds..........
console.log('Blah blah blah blah extra-blah');
//endcode.
I've also seen things like
yield sleep(2000);
But node.js doesnt recognize this....
If anybody is willing to help, it is so much appreciated.
Put the code that you want executed after the delay within the setTimeout callback:
console.log('Welcome to My Console,');
setTimeout(function() {
console.log('Blah blah blah blah extra-blah');
}, 3000);
Best way to do this is to break your code into multiple functions, like this:
function1() {
// stuff you want to happen right away
console.log('Welcome to My Console,');
}
function2() {
// all the stuff you want to happen after that pause
console.log('Blah blah blah blah extra-blah');
}
// call the first chunk of code right away
function1();
// call the rest of the code and have it execute after 3 seconds
setTimeout(function2, 3000);
It's similar to JohnnyHK's solution, but much neater and easier to extend.
This question is quite old, but recently V8 has added Generators which can accomplish what the OP requested. Generators are generally easiest to use for async interactions with the assistance of a library such as suspend or gen-run.
Here's an example using suspend:
suspend(function* () {
console.log('Welcome to My Console,');
yield setTimeout(suspend.resume(), 10000); // 10 seconds pass..
console.log('Blah blah blah blah extra-blah');
})();
Related reading (by way of shameless self promotion): What's the Big Deal with Generators?.
I've recently created simpler abstraction called wait.for to call async functions in sync mode (based on node-fibers). There is also a version based on upcoming ES6 Generators.
https://github.com/luciotato/waitfor
Using wait.for, you can call any standard nodejs async function, as if it were a sync function, without blocking node's event loop.
You can code sequentially when you need it, which is, (I'm guessing) perfect to simplify your scripts for personal use.
using wait.for your code will be:
require('waitfor')
..in a fiber..
//start-of-code
console.log('Welcome to My Console,');
wait.miliseconds(10*1000); //defined in waitfor/paralell-tests.js - DOES NOT BLOCK
console.log('Blah blah blah blah extra-blah');
//endcode.
Also any async function can be called in Sync mode. Check the examples.
Since, javascript engine (v8) runs code based on sequence of events in event-queue, There is no strict that javascript exactly trigger the execution at after specified time. That is, when you set some seconds to execute the code later, triggering code is purely base on sequence in event queue. So triggering execution of code may take more than specified time.
So Node.js follows,
process.nextTick()
to run the code later instead setTimeout(). For example,
process.nextTick(function(){
console.log("This will be printed later");
});