The scope of the outer function -> inner function • Javascript: Functional / Dynamic / Object oriented • ... packet manager / os community / V8 / ubiquitous...
(req, res, next){ var query = 'SELECT * FROM users WHERE id = ?'; cassandra.executeAsPrepared(query, [req.params.id], function (err, result) { if (err) return next(err); var row = result.rows[0]; //Response: expose some properties of the user res.json({id: req.params.id, name: row.get('name')}); }); }); 1 2 3 4 5 6 7 8 9
res, next){ var query = 'SELECT id, profile_image FROM users WHERE id = ?'; cassandra.streamField(query, [req.params.id], function (err, row, image) { if (err) return next(err); //pipe the image stream to the response stream image.pipe(res); }); }); 1 2 3 4 5 6 7 8
app.get('/user/:id/image', function (req, res, next){ var query = 'SELECT id, profile_image FROM users WHERE id = ?'; cassandra.streamField(query, [req.params.id], function (err, row, image) { if (err) return next(err); //pipe the image stream to a resizer stream image.pipe(resizer).pipe(res); }); }); 1 2 3 4 5 6 7 8
Systems by Matt Welsh, Steven D. Gribble, Eric A. Brewer, and David Culler @ UC Berkeley Concurrency is not Parallelism (it's better) by Rob Pike @Google Go lang How the single threaded non blocking IO model works in Node.js