gpt4 book ai didi

javascript - 等待回调和我们的结果数据从该函数中取出另一个进程

转载 作者:行者123 更新时间:2023-12-03 06:02:16 25 4
gpt4 key购买 nike

我有这个类(class)(产品)。

        var Product = function () {
this.products = [];
this.priceFrom = null;
this.priceTo = null;
this.countDone = 0;
};
Product.prototype = {
constructor: Product,

getProductsByPriceRange: function (priceFrom, priceTo) {
var xhrUrl = "<?= base_url('market/products/xhr_product_price_range') ?>";
var xhrData = {price_from: priceFrom, price_to: priceTo};
var xhrType = "json";

var UtilsClass = new Utils();
UtilsClass.xhrConnection(xhrUrl, xhrData, xhrType, function (data) {
/* MY DATA IS HERE */
});

},

buildList:function (products) {
for (var i = 0; i < products.length; i++) {
var product = products[i];
console.log("product");
}
},

buildOne: function (product) {

}
};

/*....more classes */

还有另一段代码(在产品类之外):

var fromPrice = data.from;
var toPrice = data.to;

var ProductClass = new Product();

var lastCountDone = ProductClass.countDone;

ProductClass.priceFrom = fromPrice;
ProductClass.priceTo = toPrice;
var myProducts = ProductClass.getProductsByPriceRange(ProductClass.priceFrom, ProductClass.priceTo);

我的问题是...我可以等待 UtilsClass.xhrConnection 的回调(在第一段中)并在第二段代码中使用回调生成的数据(在第一段中)。

任何想法对我来说都非常有值(value)。谢谢!

最佳答案

var Product = function () {
this.products = [];
this.priceFrom = null;
this.priceTo = null;
this.countDone = 0;
};
Product.prototype = {
constructor: Product,

getProductsByPriceRange: function (priceFrom, priceTo) {
var xhrUrl = "<?= base_url('market/products/xhr_product_price_range') ?>";
var xhrData = {price_from: priceFrom, price_to: priceTo};
var xhrType = "json";

var UtilsClass = new Utils();
return new Promise(function(resolve, reject){
UtilsClass.xhrConnection(xhrUrl, xhrData, xhrType, function (data) {
/* MY DATA IS HERE */
resolve(data)
});

});

},

buildList:function (products) {
for (var i = 0; i < products.length; i++) {
var product = products[i];
console.log("product");
}
},

buildOne: function (product) {

}
};

通话时,

var fromPrice = data.from;
var toPrice = data.to;

var ProductClass = new Product();

var lastCountDone = ProductClass.countDone;

ProductClass.priceFrom = fromPrice;
ProductClass.priceTo = toPrice;
var myProducts = ProductClass.getProductsByPriceRange(ProductClass.priceFrom, ProductClass.priceTo).then(function(data){%your data will be available here%});

关于javascript - 等待回调和我们的结果数据从该函数中取出另一个进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39707093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com