为什么我的功能没有达到这一点?

时间:2016-05-28 02:14:48

标签: javascript ajax reactjs promise

我已经被困了一个小时试图解决这个问题。我有一个现在看起来像

的功能
myfunction()
{
    console.log("myfunction called");//TEST
    Axios.post('someurl/' + id)
        .then(response =>
        {
            console.log("We're in the .then ...");//TEST
            onGoodRequest(response.data, response);
        })
        .catch(response =>
        {
            console.log("We're in the .catch ...");//TEST
            this.setState({ curUploadState: uploadStates.SHOWSTOPPER });
        });
    console.log("We're here now ...");//TEST
}

我在我的React组件中调用它,由于某种原因它被卡住了。控制台中没有JavaScript错误,我唯一看到的是

myfunction called

那么可能会发生什么?我甚至做过

    var x = Axios.post('someurl/' + id)
                .then(response =>
                {
                    console.log("We're in the .then ...");
                    onGoodRequest(response.data, response);
                })
                .catch(response =>
                {
                    console.log("We're in the .catch ...");
                    this.setState({ curUploadState: uploadStates.SHOWSTOPPER });
                });
    console.log(x);
    console.log("We're here now ...");

并且没有记录。

1 个答案:

答案 0 :(得分:0)

看起来你什么都没有返回,因为你的箭头功能在花括号上方。将花括号向上移动一行:

myfunction()
{
    console.log("myfunction called");//TEST
    Axios.post('someurl/' + id)
        .then(response => {
            console.log("We're in the .then ...");//TEST
            onGoodRequest(response.data, response);
        })
        .catch(response => {
            console.log("We're in the .catch ...");//TEST
            this.setState({ curUploadState: uploadStates.SHOWSTOPPER });
        });
    console.log("We're here now ...");//TEST
}

=>基本上是隐含的回报。