async/await for fetching data with spinner












0














Goal



To have a spinner running till data loads.



What I did



I followed this article
I also tried regular promise and then but no success.



What happens



the console.log is displaying "boom" right off the bat, so not waiting for data fetching. No errors.



EventPage.js



constructor (props) {
super(props);
this.state = {
panelView: true,
loading: true
}
}

async componentDidMount () {
try {
await this.props.fetchEvents();
this.setState({loading: false});
console.log("BOOM")
} catch {

}

}
render() {
const {loading} = this.state;
const {panelView} = this.state;
if (loading) {
return <Loader />
}
return (
(Actual page)
)
}


eventActionCreator fetchEvents



export const fetchEvents = () => {
return async dispatch => {
try {
const response = await axios.get('http://localhost:3090/events');
dispatch(setEvent(response.data));
return response.data;
} catch (e) {
return e;
}
}
}


The console is only to show code is waiting for fetch to execute, it doesn't.










share|improve this question
























  • Please add a live example with the JS snippet feature from Stackoverflow.
    – Christopher Dosin
    Nov 11 at 3:04










  • When you say "No success" -- what is actually happening? Are there errors in the console? Does "BOOM" get written?
    – bluejack
    Nov 11 at 3:11










  • @bluejack I added the details
    – user3241846
    Nov 11 at 4:01










  • seems no error in your code.. can you add more details or working demo?
    – ma_dev_15
    Nov 11 at 4:47










  • @ma_dev_15 yes there is... Look at the answer below along with a live demo on code sandbox... He's setting loading to false in his setState... and then a conditional to render Loading if true...
    – SakoBu
    Nov 11 at 5:07


















0














Goal



To have a spinner running till data loads.



What I did



I followed this article
I also tried regular promise and then but no success.



What happens



the console.log is displaying "boom" right off the bat, so not waiting for data fetching. No errors.



EventPage.js



constructor (props) {
super(props);
this.state = {
panelView: true,
loading: true
}
}

async componentDidMount () {
try {
await this.props.fetchEvents();
this.setState({loading: false});
console.log("BOOM")
} catch {

}

}
render() {
const {loading} = this.state;
const {panelView} = this.state;
if (loading) {
return <Loader />
}
return (
(Actual page)
)
}


eventActionCreator fetchEvents



export const fetchEvents = () => {
return async dispatch => {
try {
const response = await axios.get('http://localhost:3090/events');
dispatch(setEvent(response.data));
return response.data;
} catch (e) {
return e;
}
}
}


The console is only to show code is waiting for fetch to execute, it doesn't.










share|improve this question
























  • Please add a live example with the JS snippet feature from Stackoverflow.
    – Christopher Dosin
    Nov 11 at 3:04










  • When you say "No success" -- what is actually happening? Are there errors in the console? Does "BOOM" get written?
    – bluejack
    Nov 11 at 3:11










  • @bluejack I added the details
    – user3241846
    Nov 11 at 4:01










  • seems no error in your code.. can you add more details or working demo?
    – ma_dev_15
    Nov 11 at 4:47










  • @ma_dev_15 yes there is... Look at the answer below along with a live demo on code sandbox... He's setting loading to false in his setState... and then a conditional to render Loading if true...
    – SakoBu
    Nov 11 at 5:07
















0












0








0







Goal



To have a spinner running till data loads.



What I did



I followed this article
I also tried regular promise and then but no success.



What happens



the console.log is displaying "boom" right off the bat, so not waiting for data fetching. No errors.



EventPage.js



constructor (props) {
super(props);
this.state = {
panelView: true,
loading: true
}
}

async componentDidMount () {
try {
await this.props.fetchEvents();
this.setState({loading: false});
console.log("BOOM")
} catch {

}

}
render() {
const {loading} = this.state;
const {panelView} = this.state;
if (loading) {
return <Loader />
}
return (
(Actual page)
)
}


eventActionCreator fetchEvents



export const fetchEvents = () => {
return async dispatch => {
try {
const response = await axios.get('http://localhost:3090/events');
dispatch(setEvent(response.data));
return response.data;
} catch (e) {
return e;
}
}
}


The console is only to show code is waiting for fetch to execute, it doesn't.










share|improve this question















Goal



To have a spinner running till data loads.



What I did



I followed this article
I also tried regular promise and then but no success.



What happens



the console.log is displaying "boom" right off the bat, so not waiting for data fetching. No errors.



EventPage.js



constructor (props) {
super(props);
this.state = {
panelView: true,
loading: true
}
}

async componentDidMount () {
try {
await this.props.fetchEvents();
this.setState({loading: false});
console.log("BOOM")
} catch {

}

}
render() {
const {loading} = this.state;
const {panelView} = this.state;
if (loading) {
return <Loader />
}
return (
(Actual page)
)
}


eventActionCreator fetchEvents



export const fetchEvents = () => {
return async dispatch => {
try {
const response = await axios.get('http://localhost:3090/events');
dispatch(setEvent(response.data));
return response.data;
} catch (e) {
return e;
}
}
}


The console is only to show code is waiting for fetch to execute, it doesn't.







reactjs redux async-await es6-promise






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 4:00

























asked Nov 11 at 3:00









user3241846

1901315




1901315












  • Please add a live example with the JS snippet feature from Stackoverflow.
    – Christopher Dosin
    Nov 11 at 3:04










  • When you say "No success" -- what is actually happening? Are there errors in the console? Does "BOOM" get written?
    – bluejack
    Nov 11 at 3:11










  • @bluejack I added the details
    – user3241846
    Nov 11 at 4:01










  • seems no error in your code.. can you add more details or working demo?
    – ma_dev_15
    Nov 11 at 4:47










  • @ma_dev_15 yes there is... Look at the answer below along with a live demo on code sandbox... He's setting loading to false in his setState... and then a conditional to render Loading if true...
    – SakoBu
    Nov 11 at 5:07




















  • Please add a live example with the JS snippet feature from Stackoverflow.
    – Christopher Dosin
    Nov 11 at 3:04










  • When you say "No success" -- what is actually happening? Are there errors in the console? Does "BOOM" get written?
    – bluejack
    Nov 11 at 3:11










  • @bluejack I added the details
    – user3241846
    Nov 11 at 4:01










  • seems no error in your code.. can you add more details or working demo?
    – ma_dev_15
    Nov 11 at 4:47










  • @ma_dev_15 yes there is... Look at the answer below along with a live demo on code sandbox... He's setting loading to false in his setState... and then a conditional to render Loading if true...
    – SakoBu
    Nov 11 at 5:07


















Please add a live example with the JS snippet feature from Stackoverflow.
– Christopher Dosin
Nov 11 at 3:04




Please add a live example with the JS snippet feature from Stackoverflow.
– Christopher Dosin
Nov 11 at 3:04












When you say "No success" -- what is actually happening? Are there errors in the console? Does "BOOM" get written?
– bluejack
Nov 11 at 3:11




When you say "No success" -- what is actually happening? Are there errors in the console? Does "BOOM" get written?
– bluejack
Nov 11 at 3:11












@bluejack I added the details
– user3241846
Nov 11 at 4:01




@bluejack I added the details
– user3241846
Nov 11 at 4:01












seems no error in your code.. can you add more details or working demo?
– ma_dev_15
Nov 11 at 4:47




seems no error in your code.. can you add more details or working demo?
– ma_dev_15
Nov 11 at 4:47












@ma_dev_15 yes there is... Look at the answer below along with a live demo on code sandbox... He's setting loading to false in his setState... and then a conditional to render Loading if true...
– SakoBu
Nov 11 at 5:07






@ma_dev_15 yes there is... Look at the answer below along with a live demo on code sandbox... He's setting loading to false in his setState... and then a conditional to render Loading if true...
– SakoBu
Nov 11 at 5:07














2 Answers
2






active

oldest

votes


















0














Seems you have it backward... You're initializing your state to true and then setting it to false in cdm... Then you are checking if loading is true if so render the Loader... Of course, it's not true, you set it to false...



Change to this:



constructor (props) {
super(props);
this.state = {
panelView: true,
loading: false <- here
}
}

async componentDidMount () {
this.setState({loading: true}); <- here

try {
await this.props.fetchEvents();
this.setState({loading: false}); <- and here...
console.log("BOOM")
} catch {

}

}
render() {
const {loading} = this.state;
const {panelView} = this.state;
if (loading) {
return <Loader />
}
return (
(Actual page)
)
}


Here is a live Demo: https://codesandbox.io/s/r0w381qw3p






share|improve this answer























  • So here there is a problem `this.setState({loading: true}); 1
    – ma_dev_15
    Nov 11 at 7:27










  • @ma_dev_15 what’s the problem?
    – SakoBu
    Nov 11 at 7:40



















0














I figured out the issue. The problem was that I returned data from action creator and not a promise based action.



so, instead of



const response = await axios.get('http://localhost:3090/events');
dispatch(setEvent(response.data));
return response.data;


it should have been



return  axios.get('http://localhost:3090/events')
.then((response) => {
dispatch(setEvent(response.data));
});


Issue that helped me resolve it






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245477%2fasync-await-for-fetching-data-with-spinner%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Seems you have it backward... You're initializing your state to true and then setting it to false in cdm... Then you are checking if loading is true if so render the Loader... Of course, it's not true, you set it to false...



    Change to this:



    constructor (props) {
    super(props);
    this.state = {
    panelView: true,
    loading: false <- here
    }
    }

    async componentDidMount () {
    this.setState({loading: true}); <- here

    try {
    await this.props.fetchEvents();
    this.setState({loading: false}); <- and here...
    console.log("BOOM")
    } catch {

    }

    }
    render() {
    const {loading} = this.state;
    const {panelView} = this.state;
    if (loading) {
    return <Loader />
    }
    return (
    (Actual page)
    )
    }


    Here is a live Demo: https://codesandbox.io/s/r0w381qw3p






    share|improve this answer























    • So here there is a problem `this.setState({loading: true}); 1
      – ma_dev_15
      Nov 11 at 7:27










    • @ma_dev_15 what’s the problem?
      – SakoBu
      Nov 11 at 7:40
















    0














    Seems you have it backward... You're initializing your state to true and then setting it to false in cdm... Then you are checking if loading is true if so render the Loader... Of course, it's not true, you set it to false...



    Change to this:



    constructor (props) {
    super(props);
    this.state = {
    panelView: true,
    loading: false <- here
    }
    }

    async componentDidMount () {
    this.setState({loading: true}); <- here

    try {
    await this.props.fetchEvents();
    this.setState({loading: false}); <- and here...
    console.log("BOOM")
    } catch {

    }

    }
    render() {
    const {loading} = this.state;
    const {panelView} = this.state;
    if (loading) {
    return <Loader />
    }
    return (
    (Actual page)
    )
    }


    Here is a live Demo: https://codesandbox.io/s/r0w381qw3p






    share|improve this answer























    • So here there is a problem `this.setState({loading: true}); 1
      – ma_dev_15
      Nov 11 at 7:27










    • @ma_dev_15 what’s the problem?
      – SakoBu
      Nov 11 at 7:40














    0












    0








    0






    Seems you have it backward... You're initializing your state to true and then setting it to false in cdm... Then you are checking if loading is true if so render the Loader... Of course, it's not true, you set it to false...



    Change to this:



    constructor (props) {
    super(props);
    this.state = {
    panelView: true,
    loading: false <- here
    }
    }

    async componentDidMount () {
    this.setState({loading: true}); <- here

    try {
    await this.props.fetchEvents();
    this.setState({loading: false}); <- and here...
    console.log("BOOM")
    } catch {

    }

    }
    render() {
    const {loading} = this.state;
    const {panelView} = this.state;
    if (loading) {
    return <Loader />
    }
    return (
    (Actual page)
    )
    }


    Here is a live Demo: https://codesandbox.io/s/r0w381qw3p






    share|improve this answer














    Seems you have it backward... You're initializing your state to true and then setting it to false in cdm... Then you are checking if loading is true if so render the Loader... Of course, it's not true, you set it to false...



    Change to this:



    constructor (props) {
    super(props);
    this.state = {
    panelView: true,
    loading: false <- here
    }
    }

    async componentDidMount () {
    this.setState({loading: true}); <- here

    try {
    await this.props.fetchEvents();
    this.setState({loading: false}); <- and here...
    console.log("BOOM")
    } catch {

    }

    }
    render() {
    const {loading} = this.state;
    const {panelView} = this.state;
    if (loading) {
    return <Loader />
    }
    return (
    (Actual page)
    )
    }


    Here is a live Demo: https://codesandbox.io/s/r0w381qw3p







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 11 at 5:15

























    answered Nov 11 at 4:42









    SakoBu

    923317




    923317












    • So here there is a problem `this.setState({loading: true}); 1
      – ma_dev_15
      Nov 11 at 7:27










    • @ma_dev_15 what’s the problem?
      – SakoBu
      Nov 11 at 7:40


















    • So here there is a problem `this.setState({loading: true}); 1
      – ma_dev_15
      Nov 11 at 7:27










    • @ma_dev_15 what’s the problem?
      – SakoBu
      Nov 11 at 7:40
















    So here there is a problem `this.setState({loading: true}); 1
    – ma_dev_15
    Nov 11 at 7:27




    So here there is a problem `this.setState({loading: true}); 1
    – ma_dev_15
    Nov 11 at 7:27












    @ma_dev_15 what’s the problem?
    – SakoBu
    Nov 11 at 7:40




    @ma_dev_15 what’s the problem?
    – SakoBu
    Nov 11 at 7:40













    0














    I figured out the issue. The problem was that I returned data from action creator and not a promise based action.



    so, instead of



    const response = await axios.get('http://localhost:3090/events');
    dispatch(setEvent(response.data));
    return response.data;


    it should have been



    return  axios.get('http://localhost:3090/events')
    .then((response) => {
    dispatch(setEvent(response.data));
    });


    Issue that helped me resolve it






    share|improve this answer




























      0














      I figured out the issue. The problem was that I returned data from action creator and not a promise based action.



      so, instead of



      const response = await axios.get('http://localhost:3090/events');
      dispatch(setEvent(response.data));
      return response.data;


      it should have been



      return  axios.get('http://localhost:3090/events')
      .then((response) => {
      dispatch(setEvent(response.data));
      });


      Issue that helped me resolve it






      share|improve this answer


























        0












        0








        0






        I figured out the issue. The problem was that I returned data from action creator and not a promise based action.



        so, instead of



        const response = await axios.get('http://localhost:3090/events');
        dispatch(setEvent(response.data));
        return response.data;


        it should have been



        return  axios.get('http://localhost:3090/events')
        .then((response) => {
        dispatch(setEvent(response.data));
        });


        Issue that helped me resolve it






        share|improve this answer














        I figured out the issue. The problem was that I returned data from action creator and not a promise based action.



        so, instead of



        const response = await axios.get('http://localhost:3090/events');
        dispatch(setEvent(response.data));
        return response.data;


        it should have been



        return  axios.get('http://localhost:3090/events')
        .then((response) => {
        dispatch(setEvent(response.data));
        });


        Issue that helped me resolve it







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 22:13

























        answered Nov 11 at 22:01









        user3241846

        1901315




        1901315






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245477%2fasync-await-for-fetching-data-with-spinner%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini