如何将等待与“$.get”一起使用?

每当我尝试运行此功能时:


async function jget(url){

    await $.get(url, html => {

        return html

    })

}

我收到这样的错误:


(node:13924) UnhandledPromiseRejectionWarning: #<Object>

我怎样才能解决这个问题?


临摹微笑
浏览 65回答 1
1回答

繁星淼淼

您应该将“$.get(url, handlerFunction)”放入一个 promise 中,因为 await 期望返回一个 Promise,因此您可以像下面这样编写您的代码而不是您的代码。async function jget(url){&nbsp; &nbsp; // Note: Below variable will hold the value of the resolved promise&nbsp; &nbsp; let response = await fetchingData(url);}function fetchingData(url) {&nbsp; &nbsp; return new Promise((resolve, reject) => {&nbsp; &nbsp; &nbsp; $.get(url, html => {&nbsp; &nbsp; &nbsp; &nbsp; resolve(html)&nbsp; &nbsp; })})}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript