markdouthwaite / ortho

A bare-bones JWT authentication (+ user management) service.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Consider promisifying callbacks

EwanValentine opened this issue · comments

User.deleteOne({ username: username }, (err, msg) => {

You can convert callbacks into promises, by doing:

function getUser(id) {
	return new Promise((resolve, reject)) => {
		User.get(id, (user, err) => {
			if (err) {
				reject(err);
				return;
			}
	
			resolve(user);
		});
	});
}

// Example useage
await getUser(id);

It's more idiomatic to avoid callbacks if it can be helped, but some libraries are still callback based. Which is fine, but if you wanted to bring it into an async/await world, something like the above works.