Skip to content

Commit 7293450

Browse files
committedOct 17, 2015
Add promise examples
1 parent f2366b4 commit 7293450

File tree

15 files changed

+278
-8
lines changed

15 files changed

+278
-8
lines changed
 

‎.bowerrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"directory": "src/public/vendor"
3+
}

‎.gitignore

-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,4 @@
33
.env
44
.project
55
/node_modules/
6-
/design/
7-
/fonts/
8-
/mockups/
96
/src/public/vendor/

‎bower.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "example-1",
3+
"version": "0.0.0",
4+
"authors": [
5+
"Dimitrios C. Michalakos <dimitris@jmike.gr>"
6+
],
7+
"license": "MIT",
8+
"dependencies": {
9+
"bluebird": "~2.10.2"
10+
}
11+
}

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"private": true,
2020
"dependencies": {
2121
"babel": "^5.8.23",
22-
"hapi": "^10.0.0"
22+
"hapi": "^10.0.0",
23+
"inert": "^3.1.0"
2324
}
2425
}

‎src/public/promise-1/index.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!doctype html>
2+
<html class="no-js" lang="">
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
6+
<title>History of async JS - Example 1</title>
7+
<meta name="description" content="" />
8+
<meta name="viewport" content="width=device-width, initial-scale=1" />
9+
<link rel="apple-touch-icon" href="apple-touch-icon.png" />
10+
</head>
11+
<body>
12+
<script type="text/javascript" src="../vendor/bluebird/js/browser/bluebird.js"></script>
13+
<script type="text/javascript" src="./main.js"></script>
14+
</body>
15+
</html>

‎src/public/promise-1/main.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function say(q) {
2+
var resolver = function (resolve, reject) {
3+
var xhr = new XMLHttpRequest();
4+
xhr.open('GET', 'http://localhost:5000/mirror?q=' + q, true);
5+
xhr.onreadystatechange = function() {
6+
if (this.readyState == 4) {
7+
if (this.status == 200) {
8+
resolve(this.responseText);
9+
} else {
10+
reject(new Error('Unspecified error occured'));
11+
}
12+
}
13+
};
14+
xhr.send();
15+
};
16+
17+
return new Promise(resolver);
18+
}
19+
20+
console.time('timer');
21+
say('Hello')
22+
.then(function (text) {
23+
console.log(text);
24+
})
25+
.catch(function (err) {
26+
console.error(err);
27+
})
28+
.finally(function () {
29+
console.timeEnd('timer');
30+
})

‎src/public/promise-2/index.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!doctype html>
2+
<html class="no-js" lang="">
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
6+
<title>History of async JS - Example 1</title>
7+
<meta name="description" content="" />
8+
<meta name="viewport" content="width=device-width, initial-scale=1" />
9+
<link rel="apple-touch-icon" href="apple-touch-icon.png" />
10+
</head>
11+
<body>
12+
<script type="text/javascript" src="../vendor/bluebird/js/browser/bluebird.js"></script>
13+
<script type="text/javascript" src="./main.js"></script>
14+
</body>
15+
</html>

‎src/public/promise-2/main.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function say(q) {
2+
var resolver = function (resolve, reject) {
3+
var xhr = new XMLHttpRequest();
4+
xhr.open('GET', 'http://localhost:5000/mirror?q=' + q, true);
5+
xhr.onreadystatechange = function() {
6+
if (this.readyState == 4) {
7+
if (this.status == 200) {
8+
resolve(this.responseText);
9+
} else {
10+
reject(new Error('Unspecified error occured'));
11+
}
12+
}
13+
};
14+
xhr.send();
15+
};
16+
17+
return new Promise(resolver);
18+
}
19+
20+
console.time('timer');
21+
say('Hello')
22+
.then(function (text) {
23+
console.log(text);
24+
return say('mr.');
25+
})
26+
.then(function (text) {
27+
console.log(text);
28+
return say('developer');
29+
})
30+
.then(function (text) {
31+
console.log(text);
32+
})
33+
.catch(function (err) {
34+
console.error(err);
35+
})
36+
.finally(function () {
37+
console.timeEnd('timer');
38+
})

‎src/public/promise-3/index.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!doctype html>
2+
<html class="no-js" lang="">
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
6+
<title>History of async JS - Example 1</title>
7+
<meta name="description" content="" />
8+
<meta name="viewport" content="width=device-width, initial-scale=1" />
9+
<link rel="apple-touch-icon" href="apple-touch-icon.png" />
10+
</head>
11+
<body>
12+
<script type="text/javascript" src="../vendor/bluebird/js/browser/bluebird.js"></script>
13+
<script type="text/javascript" src="./main.js"></script>
14+
</body>
15+
</html>

‎src/public/promise-3/main.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function say(q) {
2+
var resolver = function (resolve, reject) {
3+
var xhr = new XMLHttpRequest();
4+
xhr.open('GET', 'http://localhost:5000/mirror?q=' + q, true);
5+
xhr.onreadystatechange = function() {
6+
if (this.readyState == 4) {
7+
if (this.status == 200) {
8+
resolve(this.responseText);
9+
} else {
10+
reject(new Error('Unspecified error occured'));
11+
}
12+
}
13+
};
14+
xhr.send();
15+
};
16+
17+
return new Promise(resolver);
18+
}
19+
20+
console.time('timer');
21+
Promise.all([
22+
say('Hello'),
23+
say('mr.'),
24+
say('developer')
25+
])
26+
.then(function (arr) {
27+
console.log(arr.join(' '));
28+
})
29+
.catch(function (err) {
30+
console.error(err);
31+
})
32+
.finally(function () {
33+
console.timeEnd('timer');
34+
})

‎src/public/promise-4/index.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!doctype html>
2+
<html class="no-js" lang="">
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
6+
<title>History of async JS - Example 1</title>
7+
<meta name="description" content="" />
8+
<meta name="viewport" content="width=device-width, initial-scale=1" />
9+
<link rel="apple-touch-icon" href="apple-touch-icon.png" />
10+
</head>
11+
<body>
12+
<script type="text/javascript" src="../vendor/bluebird/js/browser/bluebird.js"></script>
13+
<script type="text/javascript" src="./main.js"></script>
14+
</body>
15+
</html>

‎src/public/promise-4/main.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function say(q) {
2+
var resolver = function (resolve, reject) {
3+
var xhr = new XMLHttpRequest();
4+
xhr.open('GET', 'http://localhost:5000/mirror?q=' + q, true);
5+
xhr.onreadystatechange = function() {
6+
if (this.readyState == 4) {
7+
if (this.status == 200) {
8+
resolve(this.responseText);
9+
} else {
10+
reject(new Error('Unspecified error occured'));
11+
}
12+
}
13+
};
14+
xhr.send();
15+
};
16+
17+
return new Promise(resolver);
18+
}
19+
20+
console.time('timer');
21+
Promise.props({
22+
a: say('Hello'),
23+
b: say('mr.'),
24+
c: say('developer')
25+
})
26+
.then(function (props) {
27+
console.log(props.b + ' ' + props.c + ', ' + props.a);
28+
})
29+
.catch(function (err) {
30+
console.error(err);
31+
})
32+
.finally(function () {
33+
console.timeEnd('timer');
34+
})

‎src/public/promise-5/index.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!doctype html>
2+
<html class="no-js" lang="">
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
6+
<title>History of async JS - Example 1</title>
7+
<meta name="description" content="" />
8+
<meta name="viewport" content="width=device-width, initial-scale=1" />
9+
<link rel="apple-touch-icon" href="apple-touch-icon.png" />
10+
</head>
11+
<body>
12+
<script type="text/javascript" src="../vendor/bluebird/js/browser/bluebird.js"></script>
13+
<script type="text/javascript" src="./main.js"></script>
14+
</body>
15+
</html>

‎src/public/promise-5/main.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function say(q) {
2+
var resolver = function (resolve, reject) {
3+
var xhr = new XMLHttpRequest();
4+
xhr.open('GET', 'http://localhost:5000/mirror?q=' + q, true);
5+
xhr.onreadystatechange = function() {
6+
if (this.readyState == 4) {
7+
if (this.status == 200) {
8+
resolve(this.responseText);
9+
} else {
10+
reject(new Error('Unspecified error occured'));
11+
}
12+
}
13+
};
14+
xhr.send();
15+
};
16+
17+
return new Promise(resolver);
18+
}
19+
20+
console.time('timer');
21+
Promise.race([
22+
say('Mitsos'),
23+
say('Kitsos'),
24+
say('Koula')
25+
])
26+
.then(function (winner) {
27+
console.log(winner + ' wins!');
28+
})
29+
.catch(function (err) {
30+
console.error(err);
31+
})
32+
.finally(function () {
33+
console.timeEnd('timer');
34+
})

‎src/server.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
import Hapi from 'hapi';
2+
import Inert from 'inert';
23

34
const server = new Hapi.Server();
45
server.connection({port: parseInt(process.env.PORT, 10) || 5000});
56

6-
server.route([
7-
{
7+
server.register([Inert], (err) => {
8+
if (err) return console.error(err);
9+
10+
server.route({
811
method: 'GET',
912
path: '/mirror',
1013
handler: (request, reply) => {
1114
setTimeout(() => {
1215
reply(request.query.q).type('text/plain');
1316
}, 5000 * Math.random());
1417
}
15-
}
16-
]);
18+
});
19+
20+
server.route({
21+
method: 'GET',
22+
path: '/{param*}',
23+
handler: {
24+
directory: {
25+
path: `${__dirname}/public`
26+
}
27+
}
28+
});
29+
});
1730

1831
server.start(() => {
1932
console.log(`Server listening on port ${server.info.port}`);

0 commit comments

Comments
 (0)
Please sign in to comment.