promise wait to start another function












0















I have function which run under promise and I want to start another function promise resolved. But I could not do that. Below is my promise function



var promise1
$(document).ready(function() {
promise1 = new Promise(function(resolve, reject) {
getData(url).then(function(data) {
// do something
})
})
})


Now on the same page different web part we want another function run but only after finish of above function



  $(document).ready(function() {
promise1.then(function(d) {
var commentURL ="https://xxxx"
loadComments(commentURL)
})
})


but second function never runs, please help me










share|improve this question























  • call promise1 in success event of getData

    – shaghayegh sheykholeslami
    Nov 26 '18 at 8:20
















0















I have function which run under promise and I want to start another function promise resolved. But I could not do that. Below is my promise function



var promise1
$(document).ready(function() {
promise1 = new Promise(function(resolve, reject) {
getData(url).then(function(data) {
// do something
})
})
})


Now on the same page different web part we want another function run but only after finish of above function



  $(document).ready(function() {
promise1.then(function(d) {
var commentURL ="https://xxxx"
loadComments(commentURL)
})
})


but second function never runs, please help me










share|improve this question























  • call promise1 in success event of getData

    – shaghayegh sheykholeslami
    Nov 26 '18 at 8:20














0












0








0








I have function which run under promise and I want to start another function promise resolved. But I could not do that. Below is my promise function



var promise1
$(document).ready(function() {
promise1 = new Promise(function(resolve, reject) {
getData(url).then(function(data) {
// do something
})
})
})


Now on the same page different web part we want another function run but only after finish of above function



  $(document).ready(function() {
promise1.then(function(d) {
var commentURL ="https://xxxx"
loadComments(commentURL)
})
})


but second function never runs, please help me










share|improve this question














I have function which run under promise and I want to start another function promise resolved. But I could not do that. Below is my promise function



var promise1
$(document).ready(function() {
promise1 = new Promise(function(resolve, reject) {
getData(url).then(function(data) {
// do something
})
})
})


Now on the same page different web part we want another function run but only after finish of above function



  $(document).ready(function() {
promise1.then(function(d) {
var commentURL ="https://xxxx"
loadComments(commentURL)
})
})


but second function never runs, please help me







javascript jquery ajax promise






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 8:18









MilindMilind

61521541




61521541













  • call promise1 in success event of getData

    – shaghayegh sheykholeslami
    Nov 26 '18 at 8:20



















  • call promise1 in success event of getData

    – shaghayegh sheykholeslami
    Nov 26 '18 at 8:20

















call promise1 in success event of getData

– shaghayegh sheykholeslami
Nov 26 '18 at 8:20





call promise1 in success event of getData

– shaghayegh sheykholeslami
Nov 26 '18 at 8:20












2 Answers
2






active

oldest

votes


















0














Don't create a promise with new Promise when you already get a promise from a function call, like is your case with getData. Wrapping that in a new Promise callback is known as the Promise constructor antipattern. Try to avoid it.



Do it like this:



var promise1;
$(document).ready(function() {
promise1 = getData(url).then(function(data) {
// If you want to pass something on to the next `then`, then return it here:
return someData;
});
});


The other part can stay like you had it:



$(document).ready(function() {
promise1.then(function(d) {
var commentURL ="https://xxxx"
loadComments(commentURL)
});
});


This can only work if promise1 has been initialised by the first code block. If for some reason the code blocks are executed in reversed order and you cannot change this order, then wrap a resolving promise in the second code block, like this:



$(document).ready(function() {
Promise.resolve().then(function () {
return promise1;
}).then(function(d) {
var commentURL ="https://xxxx"
loadComments(commentURL)
});
});





share|improve this answer































    0














    I suspect if you gave us a more complete picture of your overall structure, we could suggest a way of doing what you're doing that would be more appropriate.



    But answering your actual question: You'd have to create the promise immediately, rather than in a ready callback, but resolve it in the ready callback.



    If you have to wait for ready before calling getData, then this is one of the rare situations where it makes sense to use new Promise even though you already get a promise from getData (because of the time delay, you need a promise immediately, but you want to wait to call getData):



    var promise1 = new Promise(function(resolve, reject) {
    $(document).ready(function() {
    getData(url).then(resolve).catch(reject);
    });
    });


    But, unless you really have to wait for ready to call getData, don't (it doesn't immediately make sense to wait to get some data until the page's DOM is complete):



    var promise1 = getData(url);





    share|improve this answer


























    • Well this works. But another function is not executing may be it is my code issue

      – Milind
      Nov 26 '18 at 8:29











    • @Milind - I'm sorry, I don't understand what you mean by that.

      – T.J. Crowder
      Nov 26 '18 at 8:36












    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%2f53477043%2fpromise-wait-to-start-another-function%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














    Don't create a promise with new Promise when you already get a promise from a function call, like is your case with getData. Wrapping that in a new Promise callback is known as the Promise constructor antipattern. Try to avoid it.



    Do it like this:



    var promise1;
    $(document).ready(function() {
    promise1 = getData(url).then(function(data) {
    // If you want to pass something on to the next `then`, then return it here:
    return someData;
    });
    });


    The other part can stay like you had it:



    $(document).ready(function() {
    promise1.then(function(d) {
    var commentURL ="https://xxxx"
    loadComments(commentURL)
    });
    });


    This can only work if promise1 has been initialised by the first code block. If for some reason the code blocks are executed in reversed order and you cannot change this order, then wrap a resolving promise in the second code block, like this:



    $(document).ready(function() {
    Promise.resolve().then(function () {
    return promise1;
    }).then(function(d) {
    var commentURL ="https://xxxx"
    loadComments(commentURL)
    });
    });





    share|improve this answer




























      0














      Don't create a promise with new Promise when you already get a promise from a function call, like is your case with getData. Wrapping that in a new Promise callback is known as the Promise constructor antipattern. Try to avoid it.



      Do it like this:



      var promise1;
      $(document).ready(function() {
      promise1 = getData(url).then(function(data) {
      // If you want to pass something on to the next `then`, then return it here:
      return someData;
      });
      });


      The other part can stay like you had it:



      $(document).ready(function() {
      promise1.then(function(d) {
      var commentURL ="https://xxxx"
      loadComments(commentURL)
      });
      });


      This can only work if promise1 has been initialised by the first code block. If for some reason the code blocks are executed in reversed order and you cannot change this order, then wrap a resolving promise in the second code block, like this:



      $(document).ready(function() {
      Promise.resolve().then(function () {
      return promise1;
      }).then(function(d) {
      var commentURL ="https://xxxx"
      loadComments(commentURL)
      });
      });





      share|improve this answer


























        0












        0








        0







        Don't create a promise with new Promise when you already get a promise from a function call, like is your case with getData. Wrapping that in a new Promise callback is known as the Promise constructor antipattern. Try to avoid it.



        Do it like this:



        var promise1;
        $(document).ready(function() {
        promise1 = getData(url).then(function(data) {
        // If you want to pass something on to the next `then`, then return it here:
        return someData;
        });
        });


        The other part can stay like you had it:



        $(document).ready(function() {
        promise1.then(function(d) {
        var commentURL ="https://xxxx"
        loadComments(commentURL)
        });
        });


        This can only work if promise1 has been initialised by the first code block. If for some reason the code blocks are executed in reversed order and you cannot change this order, then wrap a resolving promise in the second code block, like this:



        $(document).ready(function() {
        Promise.resolve().then(function () {
        return promise1;
        }).then(function(d) {
        var commentURL ="https://xxxx"
        loadComments(commentURL)
        });
        });





        share|improve this answer













        Don't create a promise with new Promise when you already get a promise from a function call, like is your case with getData. Wrapping that in a new Promise callback is known as the Promise constructor antipattern. Try to avoid it.



        Do it like this:



        var promise1;
        $(document).ready(function() {
        promise1 = getData(url).then(function(data) {
        // If you want to pass something on to the next `then`, then return it here:
        return someData;
        });
        });


        The other part can stay like you had it:



        $(document).ready(function() {
        promise1.then(function(d) {
        var commentURL ="https://xxxx"
        loadComments(commentURL)
        });
        });


        This can only work if promise1 has been initialised by the first code block. If for some reason the code blocks are executed in reversed order and you cannot change this order, then wrap a resolving promise in the second code block, like this:



        $(document).ready(function() {
        Promise.resolve().then(function () {
        return promise1;
        }).then(function(d) {
        var commentURL ="https://xxxx"
        loadComments(commentURL)
        });
        });






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 8:34









        trincottrincot

        130k1691125




        130k1691125

























            0














            I suspect if you gave us a more complete picture of your overall structure, we could suggest a way of doing what you're doing that would be more appropriate.



            But answering your actual question: You'd have to create the promise immediately, rather than in a ready callback, but resolve it in the ready callback.



            If you have to wait for ready before calling getData, then this is one of the rare situations where it makes sense to use new Promise even though you already get a promise from getData (because of the time delay, you need a promise immediately, but you want to wait to call getData):



            var promise1 = new Promise(function(resolve, reject) {
            $(document).ready(function() {
            getData(url).then(resolve).catch(reject);
            });
            });


            But, unless you really have to wait for ready to call getData, don't (it doesn't immediately make sense to wait to get some data until the page's DOM is complete):



            var promise1 = getData(url);





            share|improve this answer


























            • Well this works. But another function is not executing may be it is my code issue

              – Milind
              Nov 26 '18 at 8:29











            • @Milind - I'm sorry, I don't understand what you mean by that.

              – T.J. Crowder
              Nov 26 '18 at 8:36
















            0














            I suspect if you gave us a more complete picture of your overall structure, we could suggest a way of doing what you're doing that would be more appropriate.



            But answering your actual question: You'd have to create the promise immediately, rather than in a ready callback, but resolve it in the ready callback.



            If you have to wait for ready before calling getData, then this is one of the rare situations where it makes sense to use new Promise even though you already get a promise from getData (because of the time delay, you need a promise immediately, but you want to wait to call getData):



            var promise1 = new Promise(function(resolve, reject) {
            $(document).ready(function() {
            getData(url).then(resolve).catch(reject);
            });
            });


            But, unless you really have to wait for ready to call getData, don't (it doesn't immediately make sense to wait to get some data until the page's DOM is complete):



            var promise1 = getData(url);





            share|improve this answer


























            • Well this works. But another function is not executing may be it is my code issue

              – Milind
              Nov 26 '18 at 8:29











            • @Milind - I'm sorry, I don't understand what you mean by that.

              – T.J. Crowder
              Nov 26 '18 at 8:36














            0












            0








            0







            I suspect if you gave us a more complete picture of your overall structure, we could suggest a way of doing what you're doing that would be more appropriate.



            But answering your actual question: You'd have to create the promise immediately, rather than in a ready callback, but resolve it in the ready callback.



            If you have to wait for ready before calling getData, then this is one of the rare situations where it makes sense to use new Promise even though you already get a promise from getData (because of the time delay, you need a promise immediately, but you want to wait to call getData):



            var promise1 = new Promise(function(resolve, reject) {
            $(document).ready(function() {
            getData(url).then(resolve).catch(reject);
            });
            });


            But, unless you really have to wait for ready to call getData, don't (it doesn't immediately make sense to wait to get some data until the page's DOM is complete):



            var promise1 = getData(url);





            share|improve this answer















            I suspect if you gave us a more complete picture of your overall structure, we could suggest a way of doing what you're doing that would be more appropriate.



            But answering your actual question: You'd have to create the promise immediately, rather than in a ready callback, but resolve it in the ready callback.



            If you have to wait for ready before calling getData, then this is one of the rare situations where it makes sense to use new Promise even though you already get a promise from getData (because of the time delay, you need a promise immediately, but you want to wait to call getData):



            var promise1 = new Promise(function(resolve, reject) {
            $(document).ready(function() {
            getData(url).then(resolve).catch(reject);
            });
            });


            But, unless you really have to wait for ready to call getData, don't (it doesn't immediately make sense to wait to get some data until the page's DOM is complete):



            var promise1 = getData(url);






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 26 '18 at 8:38

























            answered Nov 26 '18 at 8:21









            T.J. CrowderT.J. Crowder

            697k12312401336




            697k12312401336













            • Well this works. But another function is not executing may be it is my code issue

              – Milind
              Nov 26 '18 at 8:29











            • @Milind - I'm sorry, I don't understand what you mean by that.

              – T.J. Crowder
              Nov 26 '18 at 8:36



















            • Well this works. But another function is not executing may be it is my code issue

              – Milind
              Nov 26 '18 at 8:29











            • @Milind - I'm sorry, I don't understand what you mean by that.

              – T.J. Crowder
              Nov 26 '18 at 8:36

















            Well this works. But another function is not executing may be it is my code issue

            – Milind
            Nov 26 '18 at 8:29





            Well this works. But another function is not executing may be it is my code issue

            – Milind
            Nov 26 '18 at 8:29













            @Milind - I'm sorry, I don't understand what you mean by that.

            – T.J. Crowder
            Nov 26 '18 at 8:36





            @Milind - I'm sorry, I don't understand what you mean by that.

            – T.J. Crowder
            Nov 26 '18 at 8:36


















            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53477043%2fpromise-wait-to-start-another-function%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







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen