简体   繁体   English

async / await函数不等待。 多个功能无法按顺序运行和完成

[英]async/await functions not waiting. Multiple functions don't run and finish in order

I'm trying to write "modern" js and avoid callbacks and promise by using async/await. 我正在尝试编写“现代”js并通过使用async / await来避免回调和承诺。 However, I must not fully understand how it works. 但是,我不能完全理解它是如何工作的。 I thought the "next" function would wait until for first function had completed before running. 我认为“下一个”功能会等到第一个功能在运行之前完成。

In my application the four functions are all ajax calls to a MySQL DB to updated records and to select other data and build a new html table to be written to the DOM. 在我的应用程序中,这四个函数都是对MySQL数据库的ajax调用,以更新记录并选择其他数据并构建一个新的html表来写入DOM。

The four functions are finishing in random order which causes the interface to be incorrect. 这四个函数以随机顺序完成,导致界面不正确。 Refreshing the entire page fixes the issue, but if the functions would just run and finish in the order I defined, the interface would be correct. 刷新整个页面可以解决问题,但如果函数只按我定义的顺序运行和完成,那么界面就是正确的。

async function reloadAllTable(workOrderId,statusId) {
    await techSelectTable(workOrderId);
    await updateWOStatus(workOrderId,statusId);
    await loadWOScheduled(statusId);
    await loadWOAssigned(statusId);
}

reloadAllTable(workOrderId,statusId);

I just came upon this, but better answered late than never for anyone else who finds this question. 我刚刚发现了这个问题,但对于发现这个问题的其他人来说,最好的答案要迟到。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

An async function can contain an await expression, that pauses the execution of the async function and waits for the passed Promise's resolution, and then resumes the async function's execution and returns the resolved value. 异步函数可以包含await表达式,它暂停执行异步函数并等待传递的Promise的解析,然后恢复异步函数的执行并返回已解析的值。

Your issue here is probably that to use async/await synxtax, the function being called must return a promise. 你的问题可能就是使用async / await synxtax,被调用的函数必须返回一个promise。 If there is no promise to await resolution, then execution continues to the next statements. 如果没有承诺等待解决,则继续执行下一个语句。 As you've stated above, these functions do not return a promise, and subsequently don't load in the desired order. 如上所述,这些函数不会返回承诺,并且随后不会按所需顺序加载。

For the code to execute in the order you've specified, you'd need to modify your functions to return a promise. 要使代码按照您指定的顺序执行,您需要修改函数以返回promise。 If the AJAX call returns a thenable you could possibly convert it into a promise, and await that promise: 如果AJAX调用返回一个thenable,你可以将它转换成一个promise,并等待该promise:

Promise.resolve( yourThenable )

For a function which follows node-callback style, one way of doing this is through the promisify-node module ( https://www.npmjs.com/package/promisify-node ): 对于遵循节点回调样式的函数,一种方法是通过promisify-node模块( https://www.npmjs.com/package/promisify-node ):

var promisify = require("promisify-node");

function myCallbackFunction(callback) {
  callback(null, true);
}

// Convert the function to return a Promise. 
var wrap = promisify(myCallbackFunction);

// Invoke the newly wrapped function. 
wrap().then(function(value) {
console.log(value === true);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM