await 不会等待下一行代码



我的代码:

async function main(collection, token, symbol) {
await collection.deleteMany({});
const priceHistory = await makePriceHistoryRequest(token, symbol, slow*2);
await insertPriceHistory(collection,priceHistory);
const res = await priceQuote(token,symbol);
await insertQuote(collection,res,symbol);
return await avgFastSlow(fast,slow,collection);
}

我的代码向2个端点发出请求,并更新MongoDB集合。最后一个代码:

return await avgFastSlow(fast,slow,collection);

从两个端点获取更新后的集合来计算快速和慢速平均值。问题是,它在进入最后一行代码之前没有等待。异步函数的意义不在于使它成为;等等">

这里很神秘。感谢的帮助

@ihoryam谢谢你的回复。这里是insertQuote函数:

function insertQuote(collection,res,symbol) {
const quote = res[symbol];
const date1 = new Date(quote.quoteTimeInLong).toLocaleDateString();
const record = {
dateInMS: quote.quoteTimeInLong,
dateFormatted: date1,
open: quote.openPrice,
high: quote.highPrice,
low: quote.lowPrice,
close: quote.regularMarketLastPrice,
volume: quote.totalVolume
}
console.log("below is from insertQuote");
console.log(record);
helper(collection,record);
}
async function helper(collection,record) {
await collection.insertOne(record);
}

为了进一步隔离这个问题,我添加了console.log来查看请求是否返回更新的值,它是:

console.log("below is from insertQuote");
console.log(record);

因此,这告诉我MongoDB的问题是在代码移动到代码的计算部分之前没有及时更新:

async function helper(collection,record) {
await collection.insertOne(record);
}

有什么想法吗?感谢

await在等待在底层异步操作完成时解析/拒绝的promise时工作得非常好。如果await不是promise,或者await是不与底层异步操作绑定的promise,那么它实际上并没有做任何有用的事情。

此外,在第一个await时,父函数将立即返回未解析的promise,并且在函数调用后将继续执行。因此,尽管promise上的await将暂停当前函数的执行,直到promise解析/拒绝为止,但它不会暂停调用代码。调用代码将从async函数返回promise,并且调用代码必须自己使用await.then()才能知道函数何时实际完成。

如果你的await没有按照你认为应该的方式等待,那么你可能没有正确地等待承诺。为了进一步帮助您,我们必须查看您正在等待的函数中的代码。这些函数可能存在实现错误。

正如您所看到的,insertQuote()函数没有返回值。所以,当你await insertQuote(...)时,await什么都不做,因为你没有等待承诺。你可以通过更改为这个来解决这个问题:

function insertQuote(collection,res,symbol) {
const quote = res[symbol];
const date1 = new Date(quote.quoteTimeInLong).toLocaleDateString();
const record = {
dateInMS: quote.quoteTimeInLong,
dateFormatted: date1,
open: quote.openPrice,
high: quote.highPrice,
low: quote.lowPrice,
close: quote.regularMarketLastPrice,
volume: quote.totalVolume
}
console.log("below is from insertQuote");
console.log(record);
return helper(collection,record);
}
function helper(collection,record) {
return collection.insertOne(record);
}

此实现假设collection.insertOne()在底层数据库操作完成时返回一个解析/拒绝的promise。

现在,insertQuote()函数返回helper()返回的promise。注意,这里helper()没有理由是async,因为return await collection.insertOne(record)return collection.insertOne(record)没有什么不同。它们都返回了一个解决/拒绝相同问题的承诺。如果它在try/catch块内,那么如果它拒绝,return await xxx()可能会成为catch块,但这里没有try/catch,所以return await xxx()不合适。您可以直接返回承诺return xxx()

如果这是insertQuote()的全部代码,那么helper()就根本没有存在的理由。你可以这样做:

function insertQuote(collection,res,symbol) {
const quote = res[symbol];
const date1 = new Date(quote.quoteTimeInLong).toLocaleDateString();
const record = {
dateInMS: quote.quoteTimeInLong,
dateFormatted: date1,
open: quote.openPrice,
high: quote.highPrice,
low: quote.lowPrice,
close: quote.regularMarketLastPrice,
volume: quote.totalVolume
}
console.log("below is from insertQuote");
console.log(record);
return collection.insertOne(record);
}

我已经编辑了这个响应,因为它没有回答这个问题,但仍然为JS中异步函数的使用提供了一些线索,所以我将不讨论它

return await在try-catch块之外是多余的。这是因为所有异步函数都返回一个Promise。因此,您需要等待函数调用。如果你这样做:

async function main(collection, token, symbol) {
await collection.deleteMany({});
const priceHistory = await makePriceHistoryRequest(token, symbol, slow*2);
await insertPriceHistory(collection,priceHistory);
const res = await priceQuote(token,symbol);
await insertQuote(collection,res,symbol);
return await avgFastSlow(fast,slow,collection);
}
const result = main(collection, token, symbol)
console.log(result);

你会看到结果实际上是一个承诺。相反,你可以这样做:

async function main(collection, token, symbol) {
await collection.deleteMany({});
const priceHistory = await makePriceHistoryRequest(token, symbol, slow * 2);
await insertPriceHistory(collection, priceHistory);
const res = await priceQuote(token, symbol);
await insertQuote(collection, res, symbol);
return avgFastSlow(fast, slow, collection);
}
(async () => {
const result = await main(collection, token, symbol);
console.log(result);
})();

现在result应该是您所期望的

最新更新