Answer the question
In order to leave comments, you need to log in
How to return a value from callbacks or terminate an external function?
Hello.
I'm trying to understand Node.js, for this I'm writing a server application - Web API.
In request handlers, the following construction is now encountered:
create(req, res, next) {
crypt('PASSWORD').hash(function(err, hash) {
if (err) {
next("ERROR!");
// ! Здесь нужно завершить дальнейшее выполнение ф-ии create()
}
var newUser = new users.User();
newUser.save(function (err) {
if (err) {
next("ERROR!");
// ! Здесь нужно завершить дальнейшее выполнение ф-ии create()
} else {
res.send("User has been saved!");
}
})
});
},
create(req, res, next) {
var _hash = '';
crypt('psw').hash(hash => {
if (hash) {
_hash = hash;
}
}
users.User().save(err => {
// Do something..
}
}
Answer the question
In order to leave comments, you need to log in
create(req, res, next) {
crypt('PASSWORD').hash((err, hash) => {
if (err) return next(err);
new User().save((err) => {
if (err) return next(err);
res.send("User has been saved!");
})
});
},
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question