javascript - Async/Await 与 JQuery 文档 READY

标签 javascript jquery async-await jquery-3

它适用于 document.addEventListener("DOMContentLoaded", async () => {}) 但我很好奇让它适用于 JQuery。

另外,我希望使用 Async/Await,不是 promises,因为稍后我将需要 promise 回调之外的变量。

let products = [];

$(document).ready(async function() {      // Does not work with ASYNC
    await getProducts();                  // IF i dont use async await, products = empty
    products.forEach(product => console.log(product))
})

const getProducts = () => {
   // Ajax call to server
   // products = ajaxResult;              // asign variable to result
   // returns Promise;
}

最佳答案

问题是由于 JQuery 版本 3.2.1 和可能在某些较低版本中造成的。 $(document).ready(async function() {}) 不过可以在以后的版本中使用。

$(document).ready( handler ) is deprecated as @Phil mentioned below.

$(handler) is recommended to use instead. JQuery docs

关于javascript - Async/Await 与 JQuery 文档 READY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69984178/

相关文章:

javascript - TypeScript:按顺序发送多个 HTTP 请求 Angular2

javascript - ES6 导入在终端上运行时出现意外标识符语法错误

javascript - 如何使用 jquery 在运行时创建 json 对象数组?

jquery - 图像处理编程

c# - 异步等待进度条和跨线程问题

C# 定时器和网络调用并行

javascript - 悬停指针在 AmCharts 股票图表中不起作用

javascript - 使用 jQuery Mobile 时无法以编程方式选中复选框

javascript - 使用 $.each() 引用对象中的数组?

c# - 有没有办法创建一个将 Action 的内容包装在 using 语句中的 ActionFilter?