为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

临摹微笑 2023-01-06 09:39:39
每当我尝试运行此功能时:async function jget(url){    await $.get(url, html => {        return html    })}我收到这样的错误:(node:13924) UnhandledPromiseRejectionWarning: #<Object>我怎样才能解决这个问题?
查看完整描述

1 回答

?
繁星淼淼

TA贡献1775条经验 获得超11个赞

您应该将“$.get(url, handlerFunction)”放入一个 promise 中,因为 await 期望返回一个 Promise,因此您可以像下面这样编写您的代码而不是您的代码。


async function jget(url){

    // Note: Below variable will hold the value of the resolved promise

    let response = await fetchingData(url);

}


function fetchingData(url) {

    return new Promise((resolve, reject) => {

      $.get(url, html => {

        resolve(html)

    })

})

}


查看完整回答
反对 回复 2023-01-06
  • 1 回答
  • 0 关注
  • 63 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信