Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
–
There's a fundamental paradigm shift when you think in the Node.js way.
As had been written and said about in node " Everything runs in parallel except your code" . JS is single threaded and hence if you make that thread sleep , everything blocks.
But if you model your problem in a natural way , it would be to design an async operation that would take its time to run and when its finished let it inform you of the same. Rather than you waiting for it to finish.
This you would design your async (performAsync) operation to emit events and then provide a callback to be performed when that event occurs.
So it's even more compact and natural. Your code might look like
performAsync().on('result',function cb () {// do what pleases you});
check hyperpolyglot. It's a page that provides summaries of certain terms and concepts across various language classes.
The scripting page shows you, among other things, how to use a while
loop in JS, Python, Ruby and PHP.
Node.js, as you guess, its a javascript file. So you can use javascript codes. But there is a few differ about what should you use. In exmaple; with Node.js you can want to use sync while;
var page = 2;
var last_page = 100;
(function loop() {
if (page <= last_page) {
request("/data?page=" + page, function (error, response, body) {
if (!error && response.statusCode == 200) {
store_data(body)
page++;
loop();
}());
On the example we call loop() function in loop(), so not on technically but on practically we are using loop.
Async example : async for loop in node.js
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.