diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a860310 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/** diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..e69de29 diff --git a/example.1.js b/example.1.js new file mode 100644 index 0000000..176dfc0 --- /dev/null +++ b/example.1.js @@ -0,0 +1,29 @@ +/*jshint node: true, -W032 */ +'use strict'; + +function sleep(time) { + var stop = new Date().getTime(); + while(new Date().getTime() < stop + time) { + ; + } +}; + +var longJob = function(time, id) { + console.log('Start: ' + id); + sleep(time); + var result = 'Done: ' + id; + return result; +}; + + +console.log('--A--'); + +var anotherResult = longJob(2000, 'TWO'); +console.log(anotherResult); + +console.log('--B--'); + +var myResult = longJob(1000, 'ONE'); +console.log(myResult); + +console.log('--C--'); \ No newline at end of file diff --git a/example.2.js b/example.2.js new file mode 100644 index 0000000..7801f25 --- /dev/null +++ b/example.2.js @@ -0,0 +1,37 @@ +/*jshint node: true, -W032 */ +'use strict'; + + +function sleep(time) { + var stop = new Date().getTime(); + while(new Date().getTime() < stop + time) { + ; + } +} + + +var longJob = function(time, id, callback) { + console.log('Start: ' + id); + + process.nextTick(function() { + sleep(time); + var result = 'Done: ' + id; + callback(null, result); + }); + +}; + +console.log('--A--'); + +longJob(2000, 'TWO', function(err, anotherResult){ + console.log(anotherResult); +}); + +console.log('--B--'); + +longJob(1000, 'ONE', function(err, myResult){ + console.log(myResult); +}); + +console.log('--C--'); + diff --git a/example.3.js b/example.3.js new file mode 100644 index 0000000..068afd5 --- /dev/null +++ b/example.3.js @@ -0,0 +1,28 @@ +/*jshint node: true, -W032 */ +'use strict'; + + +var longJob = function(time, id, callback) { + console.log('Start: ' + id); + + setTimeout(function() { + var result = 'Done: ' + id; + callback(null, result); + }, time); + +}; + +console.log('--A--'); + +longJob(2000, 'TWO', function(err, anotherResult){ + console.log(anotherResult); +}); + +console.log('--B--'); + +longJob(1000, 'ONE', function(err, myResult){ + console.log(myResult); +}); + +console.log('--C--'); + diff --git a/example.4.js b/example.4.js new file mode 100644 index 0000000..ef12101 --- /dev/null +++ b/example.4.js @@ -0,0 +1,40 @@ +/*jshint node: true, -W032 */ +'use strict'; + + +var longJob = function(time, id, callback) { + console.log('Start: ' + id); + + setTimeout(function() { + var result = 'Done: ' + id; + callback(null, result); + }, time); + +}; + +console.log('--A--'); + +longJob(1000, 'ONE', function(err, myResult){ + console.log(myResult); + + longJob(2000, 'TWO', function(err, anotherResult){ + console.log(anotherResult); + }); + +}); + + +console.log('--B--'); + +longJob(2000, 'TWO', function(err, myResult){ + console.log(myResult); + + longJob(1000, 'ONE', function(err, anotherResult){ + console.log(anotherResult); + }); + +}); + + + +console.log('--C--'); diff --git a/example.5.js b/example.5.js new file mode 100644 index 0000000..51fe16f --- /dev/null +++ b/example.5.js @@ -0,0 +1,50 @@ +/*jshint node: true, -W032, newcap:false */ +'use strict'; + +var Promise = require('bluebird'); + +var longJob = function(time, id, callback) { + console.log('Start: ' + id); + + setTimeout(function() { + var result = 'Done: ' + id; + callback(null, result); + }, time); + +}; + +var longJobP = function (time, id) { + var deferred = Promise.pending(); + longJob(time, id, function(err ,result){ + deferred.resolve(result); + }); + return deferred.promise; +}; + +console.log('--A--'); + + +Promise.resolve('') + +.then(function(myResult){ + console.log(myResult); + return longJobP(1000, 'ONE'); +}) + +.then(function(myResult){ + console.log(myResult); + return longJobP(2000, 'TWO'); +}) + + +.then(function(myResult){ + console.log(myResult); + +}) + +.catch(function(err){ + console.log(err); + +}); + +console.log('--B--'); diff --git a/example.6.js b/example.6.js new file mode 100644 index 0000000..0930c94 --- /dev/null +++ b/example.6.js @@ -0,0 +1,61 @@ +/*jshint node: true, -W032, newcap:false */ +'use strict'; + +var Promise = require('bluebird'); +var express = require('express'); +var app = express(); + +var longJob = function(time, id, callback) { + console.log('Start: ' + id); + + setTimeout(function() { + var result = 'Done: ' + id; + callback(null, result); + }, time); + +}; + +var longJobP = function (time, id) { + var deferred = Promise.pending(); + longJob(time, id, function(err ,result){ + deferred.resolve(result); + }); + return deferred.promise; +}; + + +// http://127.0.0.1:3000/ +app.get('/', function (req, res) { + console.log('Hello World'); + return res.status(200).json({ data: 'Hello World' }); +}); + +// http://127.0.0.1:3000/long/10000/ten +// http://127.0.0.1:3000/long/5000/five +app.get('/long/:ms/:id', function (req, res) { + var ms = parseInt(req.params.ms); + var id = req.params.id; + + return longJobP(ms, id) + .then(function(myResult){ + console.log(myResult); + + var data = { + myResult : myResult, + ms : ms, + id : id + } + return res.status(200).json(data); + }) + .catch(function(err){ + return res.status(500).json({error: err}); + }); + +}); + + +var server = app.listen(3000, function () { + var port = server.address().port; + console.log('Example app listening at http://127.0.0.1:%s', port); +}); + diff --git a/package.json b/package.json new file mode 100644 index 0000000..e090a64 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "swe.nodejs.examples", + "version": "2015.0.0", + "description": "Nodejs examples: callbacks and promises", + "dependencies": { + "bluebird": "3.4.6", + "express": "4.14.0" + }, + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/alemhnan/SWE.UNIPD.git" + }, + "author": "Alessandro Maccagnan (http://www.redbabel.com)", + "license": "MIT", + "bugs": { + "url": "https://github.com/alemhnan/SWE.UNIPD/issues" + }, + "homepage": "https://github.com/alemhnan/SWE.UNIPD#readme" +} \ No newline at end of file