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.