Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No Promises #8

Open
RohanSSS opened this issue Apr 19, 2019 · 1 comment
Open

No Promises #8

RohanSSS opened this issue Apr 19, 2019 · 1 comment

Comments

@RohanSSS
Copy link

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
Copy link
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants