Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
promisify server code (node 7.6)
Browse files Browse the repository at this point in the history
  • Loading branch information
sedubois committed Feb 26, 2017
1 parent 84109c0 commit 7e5e307
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
6 changes: 3 additions & 3 deletions data/auth/actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import execXhr from '../../util/xhr';
import { checkSecret } from '../../util/authSecret';
import promisify from '../../universal/promisify';

/* eslint-disable */
const Auth0Lock = process.browser && require('auth0-lock').default;
Expand All @@ -24,10 +25,9 @@ async function getTokenAndState() {
};
}

const doGetProfile = promisify(lock, 'getProfile');
async function getProfile(authToken) {
const profile = await new Promise((resolve, reject) => {
lock.getProfile(authToken, (err, res) => (err ? reject(err) : resolve(res)));
});
const profile = await doGetProfile(authToken);
return {
type: 'GET_PROFILE',
profile,
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"author": "Sebastien Dubois",
"license": "GPL-3.0",
"main": "./server",
"engines" : {
"node" : ">=7.6.0"
},
"scripts": {
"postinstall": "sh scripts/createDefaultConfig.sh",
"dev": "nodemon -w server -w universal -w yarn.lock -w next.config.js -w package.json",
Expand Down
14 changes: 6 additions & 8 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ const next = require('next');
const cache = require('./cache');
const { configSession, defaultSessionData } = require('./session');
const authApi = require('./authApi');
const promisify = require('../universal/promisify');

const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();

app.prepare().then(() => {
return app.prepare().then(async () => {
const server = express();

server.use(bodyParser.json());
Expand All @@ -19,11 +20,8 @@ app.prepare().then(() => {
server.use('/api/auth', authApi);
server.use(cache(app));
server.use((req, res) => handle(req, res));

server.listen(3000, (err) => {
if (err) {
throw err;
}
console.log('> Ready on http://localhost:3000'); // eslint-disable-line no-console
});
const port = 3000;
await promisify(server, 'listen')(port);
console.log(`> Ready on http://localhost:${port}`); // eslint-disable-line no-console
return server;
});
5 changes: 5 additions & 0 deletions universal/promisify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function promisify(obj, method) {
return (...args) => new Promise((resolve, reject) => {
obj[method](...args, (err, res) => (err ? reject(err) : resolve(res)));
});
};

0 comments on commit 7e5e307

Please sign in to comment.