callback - Node.js. Confused with non-blocking approach -


ok, seems it's hard me start thinking.. um.. functional way.. or asynchronous way.

i rather new node.js, have many years of experience in c#, java, c++..

image have simple task. idea of task each line should executed after previous line finishes (blocking operations). normal approach in, let me say, c#. also, (that reason of post) let's imagine have 1 conditional line in our code. pseudocode following:

f = openfile("aaa.txt") if f.readline(1) == "bbb" // read first line     line = f.readline(2)  // read second line else     line = readline(3)    // read third line database.insert(line)     // store line in database, lets in mysql deletefile(f) 

pretty simple. now, understand, node.js uses non-blocking approach adding callbacks each function. so, simple task seems become nightmare me. if try reproduce code above, this:

openfile(f, function() {     readline(f, 1, function(line) {         if line == "bbb" {             readline(f,2, function(line) {                 database.insert(line, function() {                     deletefile(f);                 });             });         {         else {             readline(f,3, function(line) {                 database.insert(line, function() { // same again                     deletefile(f);                 });             });         }     });      }); 

well, see point. in example, if need sure reading line occur after file opened, need write "next line's logic" in callback. , should keep following writing "next line logic" in "previous line callback). if not, may run situation when, example, try read line of file, wasn't opened yet. let me note, in actual code, use non-blocking functions, like, example:

jsdom.env(..) fs.exec(..) 

is approach in code above correct? or miss , approach totally wrong? hope there's better solution , approach.

thank time.

your approach seems correct, that's way works. , right, hard find stylish way write async code.

one tool dealing step, allows define sequence of functions acting callbacks each other:

step(   function(...) {     openfile...   },   function(...) {     readline(f, 2, this);   },   function(err, line) {     if (err) throw err;     if (line === "bbb")       database.insert(line, this);     else       database.insert(line, this);   },   function(...) {     deletefile...   } ); 

in step this acts callback , replaced following function given sequence. possible trigger parallel tasks. documentation straight forward.

probably prefer way.


or without additional tools:

there no need use anonymous functions callbacks. can define function , insert name callback, allows eliminate code duplication without additional tools. small draft:

openfile(f, function() {     readline(f, 1, function(line) {         if line == "bbb" {             readline(f,2, insertintodatabaseanddeletefile);         {         else {             readline(f,3, insertintodatabaseanddeletefile);         }     });      });  function insertintodatabaseanddeletefile(line, f) {   database.insert(line, function() {        deletefile(f);   }); } 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -