我的代碼有問題我需要從函式回傳物件陣列,但我有條件,如果我傳遞的某些 url 有我需要的資料,然后我填充陣列并使用 promise 回傳它,但如果某些 url 沒有我需要陣列所需的資料我將ajax請求發送到url以獲取該資料并填充陣列并回傳它。問題是當我有需要傳遞的資料的 url 并且該 url 沒有我需要傳遞的資料時,我的陣列只回傳在 url 中有我需要的資料的資料物件。我如何等待 url 中不需要資料的資料,然后回傳陣列,這是代碼。回傳的陣列的長度與傳遞的 url 資料和我不需要的資料是 1。沒有等待 ajax 資料被推送到陣列中。
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);
});
uj5u.com熱心網友回復:
我在這里找到了答案
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);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/363957.html
標籤:javascript 查询 阿贾克斯 承诺
