Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alemhnan committed Oct 27, 2016
0 parents commit 1f5733e
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/**
Empty file added Readme.md
Empty file.
29 changes: 29 additions & 0 deletions example.1.js
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--');
37 changes: 37 additions & 0 deletions example.2.js
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--');

28 changes: 28 additions & 0 deletions example.3.js
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--');

40 changes: 40 additions & 0 deletions example.4.js
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--');
50 changes: 50 additions & 0 deletions example.5.js
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--');
61 changes: 61 additions & 0 deletions example.6.js
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);
});

23 changes: 23 additions & 0 deletions package.json
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"
}

0 comments on commit 1f5733e

Please sign in to comment.