Javascript 为什么调用另一个异步函数的异步函数返回的是Promise{<;pending>;},而不是值?
我有一个异步函数,它调用其中的另一个异步函数。 此异步函数返回的是“Javascript 为什么调用另一个异步函数的异步函数返回的是Promise{<;pending>;},而不是值?,javascript,node.js,asynchronous,promise,async-await,Javascript,Node.js,Asynchronous,Promise,Async Await,我有一个异步函数,它调用其中的另一个异步函数。 此异步函数返回的是“”而不是值。下面是我得到的 temp [ Promise { <pending> }, Promise { <pending> }, Promise { <pending> }, Promise { <pending> } ] 如您所见,我正在调用updateAllProducts函数并将该值存储到一个名为newAllProducts的变量中。updateAllP
temp [
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> }
]
如您所见,我正在调用updateAllProducts函数并将该值存储到一个名为newAllProducts的变量中。updateAllProducts是另一个异步函数
以下是updateAllProducts的代码:
const updateAllProducts = async (allProducts) => {
const temp = await allProducts.map(async (product, index) => {
const url = product.url;
const { currentPrice, newSizes } = await scrapeData(url);
const { currentAvailableSizes, unavailableSizes } = newSizes;
const long = product.availableSizes.length >= currentAvailableSizes.length ? product.availableSizes : currentAvailableSizes;
const short = product.availableSizes.length <= currentAvailableSizes.length ? product.availableSizes : currentAvailableSizes;
const availableSizesDiff = long.filter(x => short.indexOf(Number(x)) === -1);
product.currentPrice = currentPrice;
product.priceDiff = 1000;
product.currentAvailableSizes = currentAvailableSizes;
product.availableSizesDiff = availableSizesDiff;
});
return temp;
}
使用该映射,您将获得一个
Promise
s数组,其中Promise.all()
非常适合。它在所有承诺完成后完成,并提供结果列表。正如评论中已经指出的那样,将对其作进一步解释
在您的具体情况下,这应该可以做到:
const updateAllProducts = async (allProducts) => {
const temp = await Promise.all(allProducts.map(async (product, index) => {
const url = product.url;
const { currentPrice, newSizes } = await scrapeData(url);
const { currentAvailableSizes, unavailableSizes } = newSizes;
const long = product.availableSizes.length >= currentAvailableSizes.length ? product.availableSizes : currentAvailableSizes;
const short = product.availableSizes.length <= currentAvailableSizes.length ? product.availableSizes : currentAvailableSizes;
const availableSizesDiff = long.filter(x => short.indexOf(Number(x)) === -1);
product.currentPrice = currentPrice;
product.priceDiff = 1000;
product.currentAvailableSizes = currentAvailableSizes;
product.availableSizesDiff = availableSizesDiff;
}));
return temp;
}
const updateAllProducts=async(所有产品)=>{
const temp=wait Promise.all(allProducts.map)(异步(产品,索引)=>{
const url=product.url;
const{currentPrice,newSizes}=等待数据(url);
const{currentAvailableSizes,unavailableSizes}=newSizes;
const long=product.availableSizes.length>=currentAvailableSizes.length?product.availableSizes:currentAvailableSizes;
const short=product.availableSizes.length short.indexOf(Number(x))=-1);
product.currentPrice=当前价格;
product.priceDiff=1000;
product.currentAvailableSizes=currentAvailableSizes;
product.availableSizesDiff=availableSizesDiff;
}));
返回温度;
}
async
函数总是返回一个承诺。您不能等待一个.map
这样的方法。它不会等待承诺。您缺少一个承诺。您的allProducts.map(…)
周围的所有。虽然此代码可能提供问题的解决方案,但强烈建议您提供有关此代码为什么和/或如何回答问题的其他上下文。从长远来看,纯代码的答案通常会变得毫无用处,因为未来遇到类似问题的观众无法理解解决方案背后的原因。你是对的,我忽略了这样做,因为它已经出现在评论中了。我现在就修。
const scrapeData = async (url) => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const currentPrice = await page.evaluate(() => {
if(document.querySelectorAll('[data-test="product-price-reduced"]').length !== 0) {
return document.querySelectorAll('[data-test="product-price-reduced"]')[0].textContent;
} else {
return document.querySelectorAll('[data-test="product-price"]')[0].textContent;
}
});
const newProductInfo = { currentPrice };
return newProductInfo;
}
const updateAllProducts = async (allProducts) => {
const temp = await Promise.all(allProducts.map(async (product, index) => {
const url = product.url;
const { currentPrice, newSizes } = await scrapeData(url);
const { currentAvailableSizes, unavailableSizes } = newSizes;
const long = product.availableSizes.length >= currentAvailableSizes.length ? product.availableSizes : currentAvailableSizes;
const short = product.availableSizes.length <= currentAvailableSizes.length ? product.availableSizes : currentAvailableSizes;
const availableSizesDiff = long.filter(x => short.indexOf(Number(x)) === -1);
product.currentPrice = currentPrice;
product.priceDiff = 1000;
product.currentAvailableSizes = currentAvailableSizes;
product.availableSizesDiff = availableSizesDiff;
}));
return temp;
}