I am having trouble with this code:
// var env_array = ["env1", "env2", "env3", "env4"];
Promise.all(env_array.map(function(env) {
return device_get_env(env).catch(function(err) { return err });
})).then(function(data) {
console.log(data);
data.forEach(function(entry) {
console.log(entry.data.connected);
});
}).catch(function(data) {
console.log(data);
});
function device_get_env(env) {
var env = ...;
var device_id = ...;
return get_token_data(env, 0).then(function(data) {
var url_base = ... ;
return $.ajax({
url: url_base,
method: "GET",
dataType: 'json',
headers: {Authorization: data.token_type + " " + data.access_token}
});
});
}
function get_token_data(env, auth) {
var client_id = env_tokens[env].client_id;
var client_secret = env_tokens[env].client_secret;
var audience = auth == 1 ? "https://" + env + ".xxxx.com/api/v2/" : "yyyy.com";
return $.ajax({
url: "https://" + env + ".xxxx.com/oauth/token",
method: "POST",
data: {
"client_id": client_id,
"client_secret": client_secret,
"audience": audience,
"grant_type": "client_credentials"
},
dataType: 'json'
});
}
Basically I need to iterate over env_array
and find device items in some of my environments.
device_get_env() returns AJAX call, which could be a success/200 or error/404.
Thus my Promises.all won't return unless all promises are resolved.
I've been digging how to overcome this.
Been trying to implement this solution: https://stackoverflow.com/a/30378082/1913289, but I'm having this error here:
TypeError: device_get_env(env).catch is not a function. (In 'device_get_env(env).catch(function(err) {return err} )', 'device_get_env(env).catch' is undefined)
Any way to solve this with my code here?
UPD: implementation suggested by @Bergi
function get_token_data(env, auth) {
var client_id = env_tokens[env].client_id;
var client_secret = env_tokens[env].client_secret;
var audience = auth == 1 ? "https://" + env + ".xxxx.com/api/v2/" : "yyyy.com";
return Promise.resolve(
$.ajax({
url: "https://" + env + ".xxxx.com/oauth/token",
method: "POST",
data: {
"client_id": client_id,
"client_secret": client_secret,
"audience": audience,
"grant_type": "client_credentials"
},
dataType: 'json'
})
)
}
function device_get_env(env) {
var env = ...;
var device_id = ...;
return get_token_data(env, 0).then(function(data) {
var url_base = ... ;
return Promise.resolve(
$.ajax({
url: url_base,
method: "GET",
dataType: 'json',
headers: { Authorization: data.token_type + " " + data.access_token }
})
)
});
}
Promise.all(env_array.map(function(env) {
return device_get_env(env).then(null, function(err) { return err });
})).then(function(data) {
console.log(data);
}).catch(function(data) {
console.log(data);
});
UPD1:
Promise.all(env_array.map(function(env) {
return device_get_env(env).catch(function(err) {return err} );
})).then(function(data) {
console.log(data);
}).catch(function(data) {
console.log(data);
});
FINAL UPD: I used fetch: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Supplying_request_options
to replace my AJAX http requests.
The Promise returned from fetch() won’t reject on HTTP error status even > if the response is an HTTP 404 or 500. Instead, it will resolve normally > (with ok status set to false), and it will only reject on network failure > or if anything prevented the request from completing.
Promise.all example:
Promise.all(env_array.map(function(env) {
return device_get_env(env);
})).then(function(data) {
console.log(data);
}).catch(function(data) {
console.log(data);
});
fetch example:
function get_token_data(env, auth) {
var client_id = env_tokens[tenant].client_id;
var client_secret = env_tokens[env].client_secret;
var audience = auth == 1 ? "https://" + env + ".xxxx.com/api/v2/" : "yyyy.com";
var url_base = "https://" + env + ".xxxx.com/oauth/token";
var myInit = {
method: 'POST',
mode: 'cors',
dataType: 'json',
cache: 'default',
data: {
"client_id": client_id,
"client_secret": client_secret,
"audience": audience,
"grant_type": "client_credentials"
}
};
return fetch(url_base, myInit);
}