Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
21
rated 0 times [  27] [ 6]  / answers: 1 / hits: 17764  / 8 Years ago, sat, december 31, 2016, 12:00:00
function  f () {
return new Promise(function (resolve, reject) {

resolve(4);
})
}

function g () {
return f().then((res) => {return res;})

}

console.log(g());


This returns Promise { <pending> }



If I returned res(in the then) and then returned f(), why isn't the output 4?


More From » node.js

 Answers
189

a valid answer would be:



function f() {
return new Promise(function(resolve, reject) {

resolve(4);
})
}

function g() {
return f().then((res) => {
return res;
})
.then((res) =>{
console.log(res);
})

}
g()


Why? Any time you return from inside a then statement in a promise, it passes it to the next statement (then or catch). Try commenting out return res and you'll see that it prints undefined.



==============

However, with ES7 we can use async/await. We can replicate the above using the following code:



function f() {
return new Promise(function(resolve, reject) {
resolve(4);
});
}

async function g() {
var a = await f();
// do something with a ...
console.log(a);
}

g();


It's important to note that console.log(g()) still returns a promise. This is because in the actual function g, resolving the promise gets delayed and therefore doesn't block the rest of our code from executing but the function body can utilize the returned value from f.



NOTE: to run this you need node 7 and it should be executed with the --harmony-async-await option.



===========

EDIT to include new code snippet

Look at the following code. You must use then to access the previous objects - however, where you access it in this case is up to you. You can call then on each promise inside of Promise.all, in this case .then((userVictories) => ...).then(...) or once Promise.all returns. It's important to note that Promise.all returns once all promises it contains resolve.



var membersArray = groupFound.members;
Promise.all(membersArray.map((member) => {
return db.doneTodo.find({ 'victor._id': member._id }).then((userVictories) => {
return {
email: member.email,
victories: userVictories.length,
}
}).then(obj => {
/*
obj is each object with the signature:
{email: '', victories: ''}

calling this then is optional if you want to process each object
returned from '.then((userVictories) =>)'

NOTE: this statement is processed then *this* promise resolves

We can send an email to each user with an update
*/
});
}))
.then((arr) => {
/*
arr is an array of all previous promises in this case:
[{email: '', victories: ''}, {email: '', victories: ''}, ...]

NOTE: this statement is processed when all of the promises above resolve.

We can use the array to get the sum of all victories or the
user with the most victories
*/
})

[#59504] Wednesday, December 28, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryanulyssesb

Total Points: 91
Total Questions: 105
Total Answers: 102

Location: England
Member since Tue, Sep 8, 2020
4 Years ago
ryanulyssesb questions
Sat, Mar 20, 21, 00:00, 3 Years ago
Mon, Sep 14, 20, 00:00, 4 Years ago
Mon, Mar 9, 20, 00:00, 4 Years ago
Sun, Jul 7, 19, 00:00, 5 Years ago
;