Open RohanSSS opened 5 years ago
Haven't used any promises you can rewrite pretty much anything that uses a callback to use promises instead eg.
const actionQuery = cb => { dbConnection.query('SELECT * FROM actions', (err, res) => { if (err) return cb(err); cb(null, res.rows); }); };
could be rewritten to
const actionQuery = new Promise((resolve, reject) => { dbConnection.query('SELECT * FROM actions') .then(res => resolve(res.rows)) .catch(err => reject(err)) });
This can then be called using promises as well so instead of doing
getData.actionQuery((err, actions) => { if (err) return serverError(err, response); response.writeHead(200, {'Content-Type' : 'application/json'}); response.end(JSON.stringify(actions)) });
you could do
getData.actionQuery .then(actions => { response.writeHead(200, {'Content-Type' : 'application/json'}); response.end(JSON.stringify(actions)) }) .catch(err => serverError(err, response))
Thanks!
Haven't used any promises you can rewrite pretty much anything that uses a callback to use promises instead eg.
could be rewritten to
This can then be called using promises as well so instead of doing
you could do