-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1f5733e
Showing
9 changed files
with
269 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/** |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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--'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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--'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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--'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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--'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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--'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <alessandro.maccagnan@gmail.com> (http://www.redbabel.com)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/alemhnan/SWE.UNIPD/issues" | ||
}, | ||
"homepage": "https://github.com/alemhnan/SWE.UNIPD#readme" | ||
} |