Reputation: 7919
in my application i work with mongoose and mongodb , in one scenario i need to calling promise from another promise, but promise return undefined, my code is
function first (num) {
return new Promise(function(resolve,reject){
if(num % 2 === 0){
resolve(num);
}
else{
reject('Error Happend');
}
})
}
function second(num){
first(num).then(function(res){
return res;
}).catch(function(err){
return err;
})
}
console.log(second(2))
<script src="https://cdn.jsdelivr.net/bluebird/latest/bluebird.js"></script>
in this case i pass 2
to second
function and i want it return 2 but it return undefined, i search on SF and find some topic about this but nothing of them solve my problem.
Upvotes: 0
Views: 75
Reputation: 943207
You are logging the return value of second(2)
, but the second
function has no return
statement in it, so it will always return undefined
.
Note that the function expressions you pass to then
and catch
(which do have return
statements are not the second
function itself).
Upvotes: 2