feat: async/await

v1.18.x
Barış Soner Uşaklı 6 years ago
parent 3cc7ec63e8
commit 7beef91c3f

@ -1,16 +1,13 @@
'use strict'; 'use strict';
const async = require('async');
const nconf = require('nconf'); const nconf = require('nconf');
const db = require('../database'); const db = require('../database');
module.exports.ping = function (req, res, next) { module.exports.ping = async function (req, res, next) {
async.waterfall([ try {
function (next) { await db.getObject('config');
db.getObject('config', next); res.status(200).send(req.path === nconf.get('relative_path') + '/sping' ? 'healthy' : '200');
}, } catch (err) {
function () { next(err);
res.status(200).send(req.path === nconf.get('relative_path') + '/sping' ? 'healthy' : '200'); }
},
], next);
}; };

@ -1,49 +1,31 @@
'use strict'; 'use strict';
var async = require('async'); const posts = require('../posts');
const privileges = require('../privileges');
const helpers = require('./helpers');
var posts = require('../posts'); const postsController = module.exports;
var privileges = require('../privileges');
var helpers = require('./helpers');
var postsController = module.exports; postsController.redirectToPost = async function (req, res, next) {
const pid = parseInt(req.params.pid, 10);
postsController.redirectToPost = function (req, res, next) {
var pid = parseInt(req.params.pid, 10);
if (!pid) { if (!pid) {
return next(); return next();
} }
async.waterfall([ const [canRead, path] = await Promise.all([
function (next) { privileges.posts.can('topics:read', pid, req.uid),
async.parallel({ posts.generatePostPath(pid, req.uid),
canRead: function (next) { ]);
privileges.posts.can('topics:read', pid, req.uid, next); if (!path) {
}, return next();
path: function (next) { }
posts.generatePostPath(pid, req.uid, next); if (!canRead) {
}, return helpers.notAllowed(req, res);
}, next); }
}, helpers.redirect(res, path);
function (results, next) {
if (!results.path) {
return next();
}
if (!results.canRead) {
return helpers.notAllowed(req, res);
}
helpers.redirect(res, results.path);
},
], next);
}; };
postsController.getRecentPosts = function (req, res, next) { postsController.getRecentPosts = async function (req, res) {
async.waterfall([ const data = await posts.getRecentPosts(req.uid, 0, 19, req.params.term);
function (next) { res.json(data);
posts.getRecentPosts(req.uid, 0, 19, req.params.term, next);
},
function (data) {
res.json(data);
},
], next);
}; };

@ -38,8 +38,8 @@ module.exports = function (Posts) {
]); ]);
const paths = pids.map(function (pid, index) { const paths = pids.map(function (pid, index) {
var slug = topicData[index] ? topicData[index].slug : null; const slug = topicData[index] ? topicData[index].slug : null;
var postIndex = utils.isNumber(indices[index]) ? parseInt(indices[index], 10) + 1 : null; const postIndex = utils.isNumber(indices[index]) ? parseInt(indices[index], 10) + 1 : null;
if (slug && postIndex) { if (slug && postIndex) {
return '/topic/' + slug + '/' + postIndex; return '/topic/' + slug + '/' + postIndex;

Loading…
Cancel
Save