+ All Categories
Home > Technology > Presenting Seq for Node.js

Presenting Seq for Node.js

Date post: 07-Dec-2014
Category:
Upload: douglas-muth
View: 630 times
Download: 3 times
Share this document with a friend
Description:
An overview of Seq, a flow control module for node.js
13
PRESENTING: SEQ
Transcript
Page 1: Presenting Seq for Node.js

PRESENTING:SEQ

Page 2: Presenting Seq for Node.js

Who I am: Douglas Muth

How to contact me: /

What I do: Software Engineer

ABOUT [email protected] @dmuth

...mostly.

Page 3: Presenting Seq for Node.js

Flow control in node.js. Node.js is a callback-heavy language.

Often, it looks like this:

Also known as "boomerang code"!

THE PROBLEM

db.query("SELECT ...", function(error, results) { if (!error) { db.query("UPDATE ...", function(error, results) { if (!error) { db.query("INSERT ...", function(error, results) { // Oh God, why? } } }); }});

Page 4: Presenting Seq for Node.js

That was easy!

SEQ 101INSTALLATION

$ npm install seq

Page 5: Presenting Seq for Node.js

"this()" is the callback which goes to the next block of code

SEQ 101USAGE

var seq = require("seq");

seq().seq(function() { db.query("SELECT ...", this);

}).seq(function(results) { db.query("UPDATE ...", this);

}).seq(function(results) { db.query("INSERT ...", this); // Pretty sweet!

});

Page 6: Presenting Seq for Node.js

SEQ 101WHEN THINGS GO WRONG

seq().seq(function() { db.query("SELECT ...", this);

}).seq(function(results) { this(new Error("Shake things up a little"));

}).seq(function(results) { // This will never run db.query("INSERT ...", this);

}).catch(function(error) { // I got this!

});

Page 7: Presenting Seq for Node.js

HOW TO SEQIT'S A TRAP!

seq().seq(function() { db.query("SELECT ...", this);

}).seq(function(results) { seq().seq(function() { // Do something

}).seq(function() { // Do something else // This won't go to the outer Seq this();

}).seq(function() { // This gets called by accident

});

}).seq(function(results) { // This will never run db.query("INSERT ...", this);

});

Page 8: Presenting Seq for Node.js

SEQ 101SAFE NESTING OF SEQ

seq().seq(function() { db.query("SELECT ...", this);

}).seq(function(results) { var cb = this; // this() by any other name.. seq().seq(function() { // Do something

}).seq(function() { // Do something else // This won't go to the outer Seq cb();

}).seq(function() { // This never gets called

});

}).seq(function(results) { // This gets called db.query("INSERT ...", this);

});

Page 9: Presenting Seq for Node.js

ADVANCED SEQvar fs = require('fs');var exec = require('child_process').exec;

var Seq = require('seq');Seq() .seq(function step1() { exec('whoami', this) }) .par(function step2(who) { exec('groups ' + who, this); }) .par(function step3(who) { fs.readFile(__filename, 'ascii', this); }) .seq(function step4(groups, src) { console.log('Groups: ' + groups.trim()); console.log('This file has ' + src.length + ' bytes'); });

Order of execution is as follows:step1()step2() and step3() are executed in parallelstep4() ONLY when step2() and step3() complete

Page 10: Presenting Seq for Node.js

EVEN MORE SEQProcess an array of elements:

forEach()seqEach()parEach()

Play around with "this": this.stackthis.varsthis.into(key)this.args

Not for the faint of heart, and WAY beyond the scope of thispresentation.

Page 11: Presenting Seq for Node.js

The Seq repo:

FURTHER READING

https://github.com/substack/node-seq/

Page 12: Presenting Seq for Node.js

Feel free to clone and add your own presentations based off of!

ON THE WEBhttps://github.com/dmuth/nodejs-presentations

template.html

Page 13: Presenting Seq for Node.js

QUESTIONS?


Recommended