Im learning nodejs through building an API, this the registration part.
After trying all existing methods to write async code, i've come to this result. Is this the proper way to do it?
I first thought await/async replace Promise... Do they?
Do I really need to return Promises that way on my 2 functions?
Do I call them that way before returning the result?
import * as db from '../../db';
const addToDatabase = (req) => {
return new Promise((res, rej) => {
db.get().collection('users').insertOne({
req.body
}, (err) => {
if (err) {
rej('Unable to insert to database on registration');
}
else {
res();
}
});
});
};
const checkDuplicate = (req) => {
return new Promise( async (res, rej) => {
let collection = await db.get().collection('users');
collection.find(/* query */).toArray( (err, docs) => {
if (err) {
rej('Unable to check database.');
}
else {
if (docs.length) {
rej('Email/login already used');
}
else {
res();
}
}
});
});
};
const register = async (req, res) => {
try {
await checkDuplicate(req);
await addToDatabase(req);
return res.send({success: true, msg: 'Successful registration'});
} catch(err) {
return res.send({success: false, error: err});
}
};
export default register;
I appreciate your help!