-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.js
131 lines (103 loc) · 3.66 KB
/
start.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const express = require('express');
const app = express();
const Promise = require('bluebird');
const maio = require('./maio');
const gaia = require('./gaia');
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
});
app.get('/', function (req, res) {
res.send('Welcome to the katas around Promises!')
});
/*********************************** Katas ***********************************/
/**
* The Promise katas are centered around the Bluebird implementation. For
* reference of the API, refer to this page:
* http://bluebirdjs.com/docs/api-reference.html
*/
/**
* First Kata is to explore how a promise is created. How does it make resolution
* or rejection happen?
*
* The task is to transform the maio#getTipsPromise function which receives a callback
* into a function that returns a promise.
*/
app.get('/kata-01', function (req, res) {
// This is the traditional way of handling async code via a callback. Let's comment
// this and uncomment the next section.
maio.getTipsCallback(function (error, response, body) {
if (error) {
res.send(`Error happened while calling service: ${error}`);
return;
}
if (response.statusCode != 200) {
res.send(`Non 200 response (${response.status}) while calling service: ${response}`);
return;
}
res.json(JSON.parse(body));
});
// Uncomment the following to make it work with the promise, and modify the maio
// service to return one.
// const promise = maio.getTipsPromise();
// promise.then(function (response) {
// res.send(response);
// }).catch(function (error) {
// res.send(`Caught an error while trying to contact MAIO: ${error}`);
// });
});
/**
* Second kata is to see how we can sequence actions with the Promise API.
*/
app.get('/kata-02', function (req, res) {
// 1- Get the tips from MAIO, then pick the 1st one only.
// maio.getTipsPromise()
// 2- From the first tip, get its geo ID and get the geographic region
// with a call to GAIA: gaia.getRegion(geoId)
// 3- Append the region name as a property to the 1st tip, then return
// the enriched tip back in the response.
res.json({msg: 'Send back a response!'});
});
/**
* Third kata is to launch parallel & async execution for each individual item
* of a list.
*/
app.get('/kata-03', function (req, res) {
// 1- Get all the tips from MAIO.
// maio.getTipsPromise()...
// 2- For each tips, chain to another promise to get the
// geographic region name: enrich the tip with the property of the
// region name under `geoName` field.
// 3- Wait on all promises to end altogether, and combine them all into
// one.
// 4- Send the list of enriched tips with geo name back to the client.
res.json({msg: 'Send back a response!'});
});
/**
* Fourth kata, we'll collect all GEO names aggregated under author's name.
*
* For example:
*
* [
* {
* author: 't-eomara',
* geos: [
* 'Musée des beaux-arts',
* 'Centre Bell'
* ]
* }
* ]
*
* The goal is to make sure you don't rework any response object in-memory
* but rather treat the promise chaining as a pipeline of data.
*/
app.get('kata-04', function (req, res) {
});
/**
* Fifth kata, we'll take things in reverse. We want to a promise to interact
* with a callback. This is to test interoperability between the old-fashioned
* way with callbacks and the promise API way.
*/
app.get('/kata-05', function (req, res) {
// 1- Call the maio.getTipsPromise() method to get a promise.
// 2- Return the result via the expressjs callback!
});