0

Further question here: Promise.all(): Return a result after all Promises are resolved and/or rejected

I'm using Promise.all to iterate over:

// var env_array = ["env1", "env2", "env3", "env4"];

Promise.all(env_array.map(function(env) {
    return device_get_env(env).then(function(data) {
        var connected = data.data.connected;
        console.log(env, connected);
    });
}).then(function(data) {
    console.log(data);
}));

I have 4 different environments. I'm searching for a device in all of those 4 environments. Some may return 404, some 200, some will get attribute connected = true, some false.

Basically I want to trigger some function when connected = true, but if connected is false for environments, I will just show info from some environments, if I will return non 404, of course.

Thus I thought I might be batter gather all the results and then trigger my function basing on that results, rather then trigger function right after after iteration.

Can you advice, which way should I walk? Thanks.

UPD: I've tried this one:

Promise.all(env_array.map(function(env) {
    return device_get_env(env);
}).then(function(data) {
    console.log(data);
}));

and it throws:

TypeError: env_array.map(function(env) {
        return device_get_env(env);
    }).then is not a function. (In 'env_array.map(function(env) {
        return device_get_env(env);
    }).then(function(data) {
        console.log(data);
    })', 'env_array.map(function(env) {
        return device_get_env(env);
    }).then' is undefined)

UPD1, for some reason .then block does not get executed, as I don't see 111 in console:

Promise.all(env_array.map(function(env) {
    return device_get_env(env);
})).then(function(data) {
    console.log(111);
});

UPD2: for my case I got one or more promises rejected, and Promise.all got rejected.

Community
  • 1
  • 1
sevenfourk
  • 463
  • 4
  • 12
  • You'll want to `return` the result (200/conn., 200/disc., 404) from your promise handler so that you have all results available as `data` in that callback, and can act upon them accordingly – Bergi Mar 07 '17 at 15:08

2 Answers2

1

You need to use array of responses from Promises.all as below:

// var env_array = ["env1", "env2", "env3", "env4"];


Promise.all(env_array.map(function(env) {
    return device_get_env(env);
}) // Promise.all["PromiseEnv1", "PromiseEnv2", "PromiseEnv3", "PromiseEnv4"]
.then(function(data) {
    console.log(data); // ["404", {connected: true}, {connected: false}, "404"];
    // Now you can can process above array as you need.
}));
Yashika Garg
  • 2,346
  • 2
  • 15
  • 17
  • Yashika, thanks, I've tried something similar, and it threw me an exception above. Added an example to my question. – sevenfourk Mar 07 '17 at 16:07
  • I was assuming `device_get_env` is returning a promise as well. If it's not returning promise why do you need to use `Promise.all` at all? @sevenfourk – Yashika Garg Mar 08 '17 at 15:28
  • Yashika, I basically learned from this thread and created another further question here: http://stackoverflow.com/questions/42673551/promise-all-return-a-result-after-all-promises-are-resolved-and-or-rejected. Thanks for you help. – sevenfourk Mar 08 '17 at 17:14
1

You have a problem with the position of one parenthesis:

Promise.all(env_array.map(function(env) {
  ...
}).then(function(data) {
  ...
})); // <- Move this up

It should be:

Promise.all(env_array.map(function(env) {
  ...
})).then(function(data) { // <- here
  ...
});
Ates Goral
  • 137,716
  • 26
  • 137
  • 190
  • Ates, thanks. But some for reason .then block does get executed. I've added simple console.log(111) and I don't see it. Added UPD1 snippet to my question. – sevenfourk Mar 07 '17 at 16:40
  • You should also add a `.catch()` handler to see if there are any errors. You could also try attaching a local `.then()` and `.catch()` handlers (like you originally had) to the `device_get_env(env)` calls to see which ones are succeeding/failing. – Ates Goral Mar 07 '17 at 19:45