3
var arr = [1,2 ,3, 4, 5, 6, 67, 8, 10];

function f1 () {

   return arr.map(function (member) {

       console.log(member);
        return Promise.resolve(member + 2).then((result) => {return result + 2}).then
        (value => {return value + 1});

    });



}

console.log(f1());

The output is an array of Promise { <pending> } at every single index. What is the issue? I thought the return value + 1 at the end resolved the promises, so why am I getting pending?

user7361276
  • 229
  • 2
  • 4
  • 11

3 Answers3

4

There is no issue with the code, what happens is expected.

I thought the return value + 1 at the end resolved the promises

No, it's not the return itself, it's the then functionality that will resolve the promise with the return value of the callback function.

And no, it didn't resolve the promise yet, it will resolve the promises in the future. then callbacks are always called asynchronously.

so why am I getting pending?

Because the are still pending when you log them; they will get resolved immediately afterwards.

If you want to log an array of the results, use

Promise.all(f1()).then(console.log);
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Very confusing to say that `return value + 1` will "resolve the promises". This particular OP is very confused about promises, and this is his fourth post essentially asking the same question, based on the same misunderstanding of promises, specifically that it is the `return` in the `then` handler which somehow resolves the promise. I don't think we can get through to him at this point, but it would pay to be as clear as possible, that `return` does not resolve the promise, but rather provides the value with which it is resolved. –  Jan 03 '17 at 13:47
  • @torazaburo Thanks for alerting me of the issue, I have fixed the sloppy terminology. However I disagree with the duplicate vote, I think this particular question is about why he's still getting `Promise { }` instead of the fulfilled promise in the console. – Bergi Jan 03 '17 at 14:04
  • Thanks for fixing that. As far as I can tell, the root of the OP's confusion lies in the meaning of "resolve" (in his initial question, he actually asked what "resolved" means). He appears to believe that resolving a promise somehow transforms it into a pure value, so that `Promise.resolve(42).then(x => x)` would log `42`. That's why he is confused that he ends up with an array of promises, instead of an array of values, since in his mind he expects the `return` in the `then` handler to magically turn the promise into a pure value. –  Jan 03 '17 at 14:11
  • Ah, I see now. Yeah, I think we have a dupe for that as well, but I cannot find it now. – Bergi Jan 03 '17 at 14:54
  • So Promises.all takes an array of pending promises that have returned a value? .then converts it? – user7361276 Jan 04 '17 at 04:41
  • It takes an array of promises in any state, and there is no "conversion". A promise is a standalone object that can *contain* a value, nothing that becomes a value. – Bergi Jan 04 '17 at 10:11
0

You are returning an array of promises, so you need to then them, like this:

f1().map(x => x.then(console.log));
Eran Shabi
  • 14,201
  • 7
  • 30
  • 51
0

It's is expected from your code.

You're returning an array from .map, inside which you return a Promise for each item.

If you want to get the value, you can return Promise.all(your_array_promise) instead.

Tuan Anh Tran
  • 6,807
  • 6
  • 37
  • 54
  • `Promise.all` does not "get the value". It yields a promise whose value is an array of values. –  Jan 03 '17 at 13:48