From 7beef91c3f7cbb5b1ddc806ac33ea6f34c61d7fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Mon, 12 Aug 2019 21:56:09 -0400 Subject: [PATCH] feat: async/await --- src/controllers/ping.js | 17 +++++------- src/controllers/posts.js | 58 ++++++++++++++-------------------------- src/posts/topics.js | 4 +-- 3 files changed, 29 insertions(+), 50 deletions(-) diff --git a/src/controllers/ping.js b/src/controllers/ping.js index 6c4dc7f697..f51270d210 100644 --- a/src/controllers/ping.js +++ b/src/controllers/ping.js @@ -1,16 +1,13 @@ 'use strict'; -const async = require('async'); const nconf = require('nconf'); const db = require('../database'); -module.exports.ping = function (req, res, next) { - async.waterfall([ - function (next) { - db.getObject('config', next); - }, - function () { - res.status(200).send(req.path === nconf.get('relative_path') + '/sping' ? 'healthy' : '200'); - }, - ], next); +module.exports.ping = async function (req, res, next) { + try { + await db.getObject('config'); + res.status(200).send(req.path === nconf.get('relative_path') + '/sping' ? 'healthy' : '200'); + } catch (err) { + next(err); + } }; diff --git a/src/controllers/posts.js b/src/controllers/posts.js index 550324c0e0..f8d445d1a4 100644 --- a/src/controllers/posts.js +++ b/src/controllers/posts.js @@ -1,49 +1,31 @@ 'use strict'; -var async = require('async'); +const posts = require('../posts'); +const privileges = require('../privileges'); +const helpers = require('./helpers'); -var posts = require('../posts'); -var privileges = require('../privileges'); -var helpers = require('./helpers'); +const postsController = module.exports; -var postsController = module.exports; - -postsController.redirectToPost = function (req, res, next) { - var pid = parseInt(req.params.pid, 10); +postsController.redirectToPost = async function (req, res, next) { + const pid = parseInt(req.params.pid, 10); if (!pid) { return next(); } - async.waterfall([ - function (next) { - async.parallel({ - canRead: function (next) { - privileges.posts.can('topics:read', pid, req.uid, next); - }, - path: function (next) { - posts.generatePostPath(pid, req.uid, next); - }, - }, next); - }, - function (results, next) { - if (!results.path) { - return next(); - } - if (!results.canRead) { - return helpers.notAllowed(req, res); - } - helpers.redirect(res, results.path); - }, - ], next); + const [canRead, path] = await Promise.all([ + privileges.posts.can('topics:read', pid, req.uid), + posts.generatePostPath(pid, req.uid), + ]); + if (!path) { + return next(); + } + if (!canRead) { + return helpers.notAllowed(req, res); + } + helpers.redirect(res, path); }; -postsController.getRecentPosts = function (req, res, next) { - async.waterfall([ - function (next) { - posts.getRecentPosts(req.uid, 0, 19, req.params.term, next); - }, - function (data) { - res.json(data); - }, - ], next); +postsController.getRecentPosts = async function (req, res) { + const data = await posts.getRecentPosts(req.uid, 0, 19, req.params.term); + res.json(data); }; diff --git a/src/posts/topics.js b/src/posts/topics.js index f5cd8c5a32..973973686a 100644 --- a/src/posts/topics.js +++ b/src/posts/topics.js @@ -38,8 +38,8 @@ module.exports = function (Posts) { ]); const paths = pids.map(function (pid, index) { - var slug = topicData[index] ? topicData[index].slug : null; - var postIndex = utils.isNumber(indices[index]) ? parseInt(indices[index], 10) + 1 : null; + const slug = topicData[index] ? topicData[index].slug : null; + const postIndex = utils.isNumber(indices[index]) ? parseInt(indices[index], 10) + 1 : null; if (slug && postIndex) { return '/topic/' + slug + '/' + postIndex;