小编Ven*_* 97的帖子

异步/等待 JQuery 文档就绪

它可以与 JQuery 一起使用document.addEventListener("DOMContentLoaded", async () => {}),但我很好奇让它与 JQuery 一起使用。

而且,我想要使用 Async/Await,而不是 Promise,因为稍后我将需要 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;
}
Run Code Online (Sandbox Code Playgroud)

javascript jquery async-await jquery-3

9
推荐指数
1
解决办法
9056
查看次数

标签 统计

async-await ×1

javascript ×1

jquery ×1

jquery-3 ×1