/
theOnlyExample.js
46 lines (35 loc) · 1.16 KB
/
theOnlyExample.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
'use strict';
var rsvp = require('rsvp');
var express = require('express');
//var DumbAPI = require('express-dumb-api-handler');
// Use the commented line above in the real world, dummy.
var DumbAPI = require('./index');
var myApp = express();
function succeedAtWork() {
return new rsvp.Promise(function succeed(resolve) {
var withSomeData = {
'i': 'am',
'successful': 'data'
};
resolve(withSomeData);
});
}
function failAtWork() {
return new rsvp.Promise(function succeed(resolve, reject) {
var withAnErrorMessage = 'You failed';
reject(withAnErrorMessage); // This could be an object; whatever, doesn't matter.
});
}
myApp.get('/success', function success(request, response) {
var dumbAPI = new DumbAPI(response);
// See, now it's simple when your function resolves or rejects the thing
// you want your API to return
succeedAtWork().then(dumbAPI.sendSuccess, dumbAPI.sendError);
});
myApp.get('/failure', function failure(request, response) {
var dumbAPI = new DumbAPI(response);
failAtWork().then(dumbAPI.sendSuccess, dumbAPI.sendFailure);
});
var port = 8080;
myApp.listen(port);
console.log('Listening on port ' + port);