FAC-Sixteen / Week7-project-onions2.0

Week 7 authentication project
http://lost-and-fac.herokuapp.com/
0 stars 2 forks source link

No Promises #8

Open RohanSSS opened 5 years ago

RohanSSS commented 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))
jokosanyang commented 5 years ago

Thanks!