Your SlideShare is downloading. ×
0
Flow Control in Node.js         송형주
Profile● 삼성전자 소프트웨어센터  Web Convergence Lab.● 블로그  Inside.JS (http://www.nodejs-kr.  org/insidejs)
var fs = require(fs)                               Synchronous Programming  , path = ./sync.txt;var stats = fs.statSync(pa...
var fs = require(fs)                                   Asynchronous Programming  , path = ./async.txt;// check if async.tx...
Node.js programming is not easy.var open = false;setTimeout(function () {  open = true;}, 1000);while(!open) {  // wait}co...
Nested Callback Problemasync1(function(input, result1) { async2(function(result2) {   async3(function(result3) {    async4...
Flow Control Problem   for(var i = 1; i <= 1000; i++) {     fs.readFile(./+i+.txt,          function() {         // do som...
Why Flow Control?• Asynchronous Programming• Issues – Doing a bunch of things in a specific order – Knowing when task is d...
Flow Control Pattern • Serial Execution • Parallel Execution • Limited Parallel Execution
Serial Execution                   Start                 Task1                 Task2                 Task3            Cont...
Parallel Execution                   Start   Task1          Task2            Task3             Continue When tasks        ...
Flow Control Libraries• Async – providing various control function• Step – simple and easy to use• Slide – used in the npm...
Node.js Coding Conventions• Two kinds of functions : – Action Function : Take action – Callback Function : Get results• Ac...
Action                           Callback       Function               Result     Function(Action Function Developer)     ...
Action Function Example function actor (some, args, cb) {   // last argument is callback   // optional args:   if (!cb && ...
Action                 Callback Function     Result     FunctionAction Function과 CallbackFunction 사이는 결과를 어떻게 넘길 지에 대한 인터페...
Action Function Example// return true if a path is either// a symlink or a directory.function isLinkOrDir (path, cb) {  fs...
Usecases : Parallel Execution● I have a list of 10 files, and need to read all of them, and then continue when theyre all d...
function asyncMap (list, fn, cb_) {  var n = list.length  , results = []  , errState = null;    function cb (er, data) {  ...
asyncMap    list                  result                      중간 결과 저                 cb_                      장     fn   ...
Usecases : AsyncMap        function writeFiles (files, what, cb) {          asyncMap(files,              function (f, cb_)...
This implementation is fine if order doesntmatter, but what if it does?
function asyncMap(list, fn, cb_) {  var n = list.length    , results = []    , errState = null;    function cbGen (i) {   ...
usecase: Serial Execution • I have to do a bunch of things, in order. Get db credentials out of a file, read the data from...
function chain (things, cb) {  (function LOOP (i, len) {    if (i >= len)       return cb();    things[i](function (er) { ...
chainthings                         LOOP,0         things[0]       LOOP,1         things[1]       LOOP,2         things[2]...
var fs = require(fs);var async = require(async);                                             Async Module Examplevar path ...
Step Module ExampleStep(   function readSelf() {      fs.readFile(__filename, utf8, this);   },   function capitalize(err,...
Step Module ExampleStep(  // Loads two files in parallel  function loadStuff() {     fs.readFile(__filename, this.parallel...
Reference● slide module guide (Issacs) https://github.  com/isaacs/slide-flow-control/blob/master/nodejs-  controlling-flo...
Upcoming SlideShare
Loading in...5
×

Flow control in node.js

15,946

Published on

Node.js에 주요 이슈 중 하나인 Flow Control에 대해서 Octobersky.js 모임에서 발표한 자료

Published in: Technology, Business
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
15,946
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
47
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

Transcript of "Flow control in node.js"

  1. 1. Flow Control in Node.js 송형주
  2. 2. Profile● 삼성전자 소프트웨어센터 Web Convergence Lab.● 블로그 Inside.JS (http://www.nodejs-kr. org/insidejs)
  3. 3. var fs = require(fs) Synchronous Programming , path = ./sync.txt;var stats = fs.statSync(path);if (stats == undefined) { // read the contents of this file var content = fs.readFileSync(__filename);}var code = content.toString();// write the content to sync.txtvar err = fs.writeFileSync(path, code);if(err) throw err;console.log(sync.txt created!);console.log(‘Another Task’);
  4. 4. var fs = require(fs) Asynchronous Programming , path = ./async.txt;// check if async.txt existsfs.stat(path, function(err, stats) { if (stats == undefined) { // read the contents of this file fs.readFile(__filename, function(err, content) { var code = content.toString(); // write the content to async.txt fs.writeFile(path, code, function(err) { if (err) throw err; console.log(async.txt created!); }); }); }});console.log(‘Another Task’);
  5. 5. Node.js programming is not easy.var open = false;setTimeout(function () { open = true;}, 1000);while(!open) { // wait}console.log(opened!);
  6. 6. Nested Callback Problemasync1(function(input, result1) { async2(function(result2) { async3(function(result3) { async4(function(result4) { async5(function(output) { // do something with output }); }); }); });})
  7. 7. Flow Control Problem for(var i = 1; i <= 1000; i++) { fs.readFile(./+i+.txt, function() { // do something with the file } ); } do_next_part();
  8. 8. Why Flow Control?• Asynchronous Programming• Issues – Doing a bunch of things in a specific order – Knowing when task is done. – Nested Callback – Collecting result data
  9. 9. Flow Control Pattern • Serial Execution • Parallel Execution • Limited Parallel Execution
  10. 10. Serial Execution Start Task1 Task2 Task3 Continue when tasks complete
  11. 11. Parallel Execution Start Task1 Task2 Task3 Continue When tasks complete
  12. 12. Flow Control Libraries• Async – providing various control function• Step – simple and easy to use• Slide – used in the npm client code• Seq• Nue•…
  13. 13. Node.js Coding Conventions• Two kinds of functions : – Action Function : Take action – Callback Function : Get results• Action functions – last argument is always a callback function• Callback functions – first argument is always an error or null.
  14. 14. Action Callback Function Result Function(Action Function Developer) (Action Function User)
  15. 15. Action Function Example function actor (some, args, cb) { // last argument is callback // optional args: if (!cb && typeof(args) === "function") cb = args, args = []; // do something, and then: if (failed) cb(new Error("failed!")) else cb(null, optionalData) }
  16. 16. Action Callback Function Result FunctionAction Function과 CallbackFunction 사이는 결과를 어떻게 넘길 지에 대한 인터페이스가 명확히 규정되어 한다.
  17. 17. Action Function Example// return true if a path is either// a symlink or a directory.function isLinkOrDir (path, cb) { fs.lstat(path, function (er, s) { if (er) return cb(er); return cb(null, s.isDirectory() || s.isSymbolicLink()); });} isLinkorDir Result cb fs.lstat _cb er, s er, result
  18. 18. Usecases : Parallel Execution● I have a list of 10 files, and need to read all of them, and then continue when theyre all done.  ● I have a dozen URLs, and need to fetch them all, and then continue when theyre all done.●I have 4 connected users, and need to send a message to all of them, and then continue when thats done.
  19. 19. function asyncMap (list, fn, cb_) { var n = list.length , results = [] , errState = null; function cb (er, data) { if (errState) return; if (er) return cb(errState = er); results.push(data); if (--n === 0) // 모든 리스트 처리 완료시 return cb_(null, results); } // action code list.forEach(function (l) { fn(l, cb); });}
  20. 20. asyncMap list result 중간 결과 저 cb_ 장 fn cb err, err, result data
  21. 21. Usecases : AsyncMap function writeFiles (files, what, cb) { asyncMap(files, function (f, cb_) { fs.writeFile(f,what,cb_); }, cb_는 asyncMap의 내부 함수임 cb ); } writeFiles([my,file,list], "foo", cb); writeFiles asyncMapfiles cb list fn cb_ err
  22. 22. This implementation is fine if order doesntmatter, but what if it does?
  23. 23. function asyncMap(list, fn, cb_) { var n = list.length , results = [] , errState = null; function cbGen (i) { return function cb(er, data) { if (errState) return; if (er) return cb(errState = er); results[i] = data; if (-- n === 0) return cb_(null, results); } } list.forEach(function (l, i) { fn(l, cbGen(i)); });}
  24. 24. usecase: Serial Execution • I have to do a bunch of things, in order. Get db credentials out of a file, read the data from the db, write that data to another file.   • If anything fails, do not continue.
  25. 25. function chain (things, cb) { (function LOOP (i, len) { if (i >= len) return cb(); things[i](function (er) { if (er) return cb(er); LOOP(i + 1, len) }) })(0, things.length)}
  26. 26. chainthings LOOP,0 things[0] LOOP,1 things[1] LOOP,2 things[2] LOOP, 3 cb
  27. 27. var fs = require(fs);var async = require(async); Async Module Examplevar path = ./async.txt;async.waterfall([ // check if async.txt exists function(cb) { fs.stat(path, function(err, stats) { if (stats == undefined) cb(null); else console.log(async.txt exists); }); }, // read the contents of this file function(cb) { fs.readFile(__filename, function(err, content) { var code = content.toString(); cb(null, code); }); }, // write the content to async.txt function(code, cb) { fs.writeFile(path, code, function(err) { if (err) throw err; console.log(async.txt created!); }); }
  28. 28. Step Module ExampleStep( function readSelf() { fs.readFile(__filename, utf8, this); }, function capitalize(err, text) { if (err) throw err; return text.toUpperCase(); }, function showIt(err, newText) { if (err) throw err; console.log(newText); });
  29. 29. Step Module ExampleStep( // Loads two files in parallel function loadStuff() { fs.readFile(__filename, this.parallel()); fs.readFile("/etc/passwd", this.parallel()); }, // Show the result when done function showStuff(err, code, users) { if (err) throw err; console.log(code); console.log(users); })
  30. 30. Reference● slide module guide (Issacs) https://github. com/isaacs/slide-flow-control/blob/master/nodejs- controlling-flow.pdf
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×