Skip to content
This repository was archived by the owner on Aug 18, 2023. It is now read-only.

Commit be0ce6c

Browse files
committed
refactor: change callbacks to async functions
1 parent 522568b commit be0ce6c

File tree

4 files changed

+28
-32
lines changed

4 files changed

+28
-32
lines changed

common/models/coffee-shop.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
'use strict';
55

66
module.exports = function(CoffeeShop) {
7-
CoffeeShop.status = function(cb) {
8-
var currentDate = new Date();
9-
var currentHour = currentDate.getHours();
10-
var OPEN_HOUR = 6;
11-
var CLOSE_HOUR = 20;
7+
CoffeeShop.status = async function() {
8+
const currentDate = new Date();
9+
const currentHour = currentDate.getHours();
10+
const OPEN_HOUR = 6;
11+
const CLOSE_HOUR = 20;
1212
console.log('Current hour is %d', currentHour);
13-
var response;
13+
let response;
1414
if (currentHour > OPEN_HOUR && currentHour < CLOSE_HOUR) {
1515
response = 'We are open for business.';
1616
} else {
1717
response = 'Sorry, we are closed. Open daily from 6am to 8pm.';
1818
}
19-
cb(null, response);
19+
return response;
2020
};
2121
CoffeeShop.remoteMethod(
2222
'status', {

server/boot/create-sample-models.js

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,19 @@
33

44
'use strict';
55

6-
module.exports = function(app) {
7-
app.dataSources.mysqlDs.automigrate('CoffeeShop', function(err) {
8-
if (err) throw err;
6+
module.exports = async function(app) {
7+
await app.dataSources.mysqlDs.automigrate('CoffeeShop');
8+
const coffeeShops = await app.models.CoffeeShop.create([{
9+
name: 'Bel Cafe',
10+
city: 'Vancouver',
11+
}, {
12+
name: 'Three Bees Coffee House',
13+
city: 'San Mateo',
14+
}, {
15+
name: 'Caffe Artigiano',
16+
city: 'Vancouver',
17+
},
18+
]);
919

10-
app.models.CoffeeShop.create([{
11-
name: 'Bel Cafe',
12-
city: 'Vancouver',
13-
}, {
14-
name: 'Three Bees Coffee House',
15-
city: 'San Mateo',
16-
}, {
17-
name: 'Caffe Artigiano',
18-
city: 'Vancouver',
19-
}], function(err, coffeeShops) {
20-
if (err) throw err;
21-
22-
console.log('Models created: \n', coffeeShops);
23-
});
24-
});
20+
console.log('Models created: \n', coffeeShops);
2521
};

server/config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"remoting": {
66
"context": false,
77
"rest": {
8+
"handleErrors": false,
89
"normalizeHttpPath": false,
910
"xml": false
1011
},
@@ -16,7 +17,6 @@
1617
"extended": true,
1718
"limit": "100kb"
1819
},
19-
"cors": false,
20-
"handleErrors": false
20+
"cors": false
2121
}
2222
}

server/server.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33

44
'use strict';
55

6-
var loopback = require('loopback');
7-
var boot = require('loopback-boot');
6+
const loopback = require('loopback');
7+
const boot = require('loopback-boot');
88

9-
var app = module.exports = loopback();
9+
const app = module.exports = loopback();
1010

1111
app.start = function() {
1212
// start the web server
1313
return app.listen(function() {
1414
app.emit('started');
15-
var baseUrl = app.get('url').replace(/\/$/, '');
15+
const baseUrl = app.get('url').replace(/\/$/, '');
1616
console.log('Web server listening at: %s', baseUrl);
1717
if (app.get('loopback-component-explorer')) {
18-
var explorerPath = app.get('loopback-component-explorer').mountPath;
18+
const explorerPath = app.get('loopback-component-explorer').mountPath;
1919
console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
2020
}
2121
});

0 commit comments

Comments
 (0)