通过 jquery $.get() 的异步请求

帕洛克

当我使用以下代码时,输​​出是undefined

const getData = async() => {
    $.get("foo", (data) => {
        return data;
    })
}

(async () => {

    const d = await getData();
    console.log(d);

})();

http://127.0.0.1:5000/foo网站是一个json文件。

但是如果我使用以下代码,我会得到 json 文件。

(async () => {

    const getData = await $.get( "foo", (data) => {
        return data;
    });
    console.log(getData);

})();

为什么第一个没有给出正确的输出?

R4ncid

您似乎忘记了函数中的 return

const getData = async() => {
    return $.get("foo", (data) => {
        return data;
    })
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章