I have a problem with my code I need to return array of objects from function but I have criteria if some url that I passed have a data that I need then I populate array and return it with promise, but if some url doesn't have my needed data that I need for array I send ajax request to url to get that data and populate array and return it. The problem is when I have url with data that I need passed and url that url doesn't have a data that I need passed, my array returned with only the data object that have that data in url that I need. how can I wait for data that doesn't have needed data in url and then return array here is the code. The length of returned array with passed url with data and no data that I need is 1. There is not waiting for ajax data to be pushed in array.
function getProducts(product_links, product_quantity, trigger_word){
return new Promise(function(resolve, reject) {
var obj = [];
// Checking if values are equal and not empty //
if((product_links.length > 0 && product_quantity.length > 0) && (product_links.length == product_quantity.length)){
// Looping through products and getting their variant id and quantity values //
for (var i = 0; i < product_links.length; i ){
// Checking if product link as enterd value are not empty, that the same we check for product quantity //
if($(product_links[i]).val() != '' && $(product_quantity[i]).val() != '' && isUrl($(product_links[i]).val()) && $.isNumeric($(product_quantity[i]).val()) && $(product_quantity[i]).val() > 0){
// We find all products that have a variant in their link and push them to array with their quantity //
var url = new URL($(product_links[i]).val());
var search_params = url.searchParams;
if(search_params.has("variant")){
obj.push({
id: parseInt(search_params.get("variant")),
quantity: parseInt($(product_quantity[i]).val())
});
console.log("Product with variant: ", obj);
resolve(obj);
}else{
var product_json = `${$(product_links[i]).val()}.json`;
let index = i;
$.ajax({
crossDomain: true,
type: "GET",
contentType: "application/json; charset=utf-8",
url: product_json,
dataType: "jsonp",
headers:{
"Access-Control-Allow-Origin": "*"
},
success: function(res){
var first_variant_id = res['product']['variants'][0]['id'];
var product_name = res['product']['title'];
var quantity_value = parseInt($(product_quantity[index]).val());
obj.push({
id: first_variant_id,
quantity: quantity_value
});
resolve(obj);
console.log("Product name: ", product_name , "Product id: ", first_variant_id, "Quantity: ", quantity_value);
},
error:function(err) {
throw new Error("Something went wrong, please check your internet connection and try again later!");
}
});
}
}else{
throw new Error("Something went wrong, please check all product link fields and their quantity!");
}
}
}else{
throw new Error("Something went wrong, please check all product link fields and their quantity!");
}
if(trigger_word == ''){
throw new Error("Something went wrong, please check trigger word enterd field!");
}
});
}
getProducts(product_links, product_quantity, trigger_word)
.then(function (products) {
console.log(products);
console.log(products.length);
products.forEach(function(variant){
console.log(variant);
});
})
.catch(function (err){
console.log(err);
});
CodePudding user response:
I found the answer, here
async function getProducts(product_links, product_quantity, trigger_word){
var obj = [];
// Checking if values are equal and not empty //
if((product_links.length > 0 && product_quantity.length > 0) && (product_links.length == product_quantity.length)){
// Looping through products and getting their variant id and quantity values //
for (var i = 0; i < product_links.length; i ){
// Checking if product link as enterd value are not empty, that the same we check for product quantity //
if($(product_links[i]).val() != '' && $(product_quantity[i]).val() != '' && isUrl($(product_links[i]).val()) && $.isNumeric($(product_quantity[i]).val()) && $(product_quantity[i]).val() > 0){
// We find all products that have a variant in their link and push them to array with their quantity //
var url = new URL($(product_links[i]).val());
var search_params = url.searchParams;
if(search_params.has("variant")){
obj.push({
id: parseInt(search_params.get("variant")),
quantity: parseInt($(product_quantity[i]).val())
});
}else{
var product_json = `${$(product_links[i]).val()}.json`;
let index = i;
await $.ajax({
crossDomain: true,
type: "GET",
contentType: "application/json; charset=utf-8",
url: product_json,
dataType: "jsonp",
headers:{
"Access-Control-Allow-Origin": "*"
},
success: function(res){
var first_variant_id = res['product']['variants'][0]['id'];
var product_name = res['product']['title'];
var quantity_value = parseInt($(product_quantity[index]).val());
obj.push({
id: first_variant_id,
quantity: quantity_value
});
},
error:function(err) {
throw new Error("Something went wrong, please check your internet connection and disable your adblocker, and try again later!");
}
});
}
}else{
throw new Error("Something went wrong, please check all product link fields and their quantity!");
}
}
}else{
throw new Error("Something went wrong, please check all product link fields and their quantity!");
}
if(trigger_word == ''){
throw new Error("Something went wrong, please check trigger word enterd field!");
}
return obj;
}
getProducts(product_links, product_quantity, trigger_word)
.then(function (products) {
console.log(products);
})
.catch(function (err){
console.log(err);
});