.then(function) doesn't run function [duplicate]












-2
















This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers




I have this userscript for GreaseMonkey. I created a function (A) which calls an other function (B). I want to wait running the A function further and wait for the B function to return. Function B contains a button, so in fact I want A to wait for the button press.



I tried this with the async and await but that didn't work for me show I now tried to use the .then() option. In the then() I created a function (nextI) to increase the i after running function B.



function A(){
var i = 0
while (i < 3){
var data = jsonResponse[i];
var x = data.x;
var y = data.y;
B(x, y).then(
nextI(i)
)
}
)

function B(x, y){
// do some stuff
let button = document.getElementById("button");
button.addEventListener("click", () => {
console.log("Button clicked.");
return
});
}

function nextI(i){
return i + 1
}


So I want to pause A until script B is done and I clicked the button.










share|improve this question















marked as duplicate by Igor, melpomene, Taplar, Community Jan 7 at 11:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 10





    B() does not return a promise. It actually doesn't return anything

    – Taplar
    Jan 3 at 17:12






  • 1





    You cannot return to your function from within a callback.

    – creativecreatorormaybenot
    Jan 3 at 17:13











  • If B() returns the increased i, would that solve the problem?

    – George
    Jan 3 at 17:15











  • stackoverflow.com/questions/14220321/… I think you need a promise

    – sferret
    Jan 3 at 17:15











  • Which is it? Do you want to wait for B to return or for the button to be pressed? Those are very different things.

    – melpomene
    Jan 3 at 17:17
















-2
















This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers




I have this userscript for GreaseMonkey. I created a function (A) which calls an other function (B). I want to wait running the A function further and wait for the B function to return. Function B contains a button, so in fact I want A to wait for the button press.



I tried this with the async and await but that didn't work for me show I now tried to use the .then() option. In the then() I created a function (nextI) to increase the i after running function B.



function A(){
var i = 0
while (i < 3){
var data = jsonResponse[i];
var x = data.x;
var y = data.y;
B(x, y).then(
nextI(i)
)
}
)

function B(x, y){
// do some stuff
let button = document.getElementById("button");
button.addEventListener("click", () => {
console.log("Button clicked.");
return
});
}

function nextI(i){
return i + 1
}


So I want to pause A until script B is done and I clicked the button.










share|improve this question















marked as duplicate by Igor, melpomene, Taplar, Community Jan 7 at 11:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 10





    B() does not return a promise. It actually doesn't return anything

    – Taplar
    Jan 3 at 17:12






  • 1





    You cannot return to your function from within a callback.

    – creativecreatorormaybenot
    Jan 3 at 17:13











  • If B() returns the increased i, would that solve the problem?

    – George
    Jan 3 at 17:15











  • stackoverflow.com/questions/14220321/… I think you need a promise

    – sferret
    Jan 3 at 17:15











  • Which is it? Do you want to wait for B to return or for the button to be pressed? Those are very different things.

    – melpomene
    Jan 3 at 17:17














-2












-2








-2









This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers




I have this userscript for GreaseMonkey. I created a function (A) which calls an other function (B). I want to wait running the A function further and wait for the B function to return. Function B contains a button, so in fact I want A to wait for the button press.



I tried this with the async and await but that didn't work for me show I now tried to use the .then() option. In the then() I created a function (nextI) to increase the i after running function B.



function A(){
var i = 0
while (i < 3){
var data = jsonResponse[i];
var x = data.x;
var y = data.y;
B(x, y).then(
nextI(i)
)
}
)

function B(x, y){
// do some stuff
let button = document.getElementById("button");
button.addEventListener("click", () => {
console.log("Button clicked.");
return
});
}

function nextI(i){
return i + 1
}


So I want to pause A until script B is done and I clicked the button.










share|improve this question

















This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers




I have this userscript for GreaseMonkey. I created a function (A) which calls an other function (B). I want to wait running the A function further and wait for the B function to return. Function B contains a button, so in fact I want A to wait for the button press.



I tried this with the async and await but that didn't work for me show I now tried to use the .then() option. In the then() I created a function (nextI) to increase the i after running function B.



function A(){
var i = 0
while (i < 3){
var data = jsonResponse[i];
var x = data.x;
var y = data.y;
B(x, y).then(
nextI(i)
)
}
)

function B(x, y){
// do some stuff
let button = document.getElementById("button");
button.addEventListener("click", () => {
console.log("Button clicked.");
return
});
}

function nextI(i){
return i + 1
}


So I want to pause A until script B is done and I clicked the button.





This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers








javascript userscripts






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 17:14







George

















asked Jan 3 at 17:11









GeorgeGeorge

238




238




marked as duplicate by Igor, melpomene, Taplar, Community Jan 7 at 11:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Igor, melpomene, Taplar, Community Jan 7 at 11:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 10





    B() does not return a promise. It actually doesn't return anything

    – Taplar
    Jan 3 at 17:12






  • 1





    You cannot return to your function from within a callback.

    – creativecreatorormaybenot
    Jan 3 at 17:13











  • If B() returns the increased i, would that solve the problem?

    – George
    Jan 3 at 17:15











  • stackoverflow.com/questions/14220321/… I think you need a promise

    – sferret
    Jan 3 at 17:15











  • Which is it? Do you want to wait for B to return or for the button to be pressed? Those are very different things.

    – melpomene
    Jan 3 at 17:17














  • 10





    B() does not return a promise. It actually doesn't return anything

    – Taplar
    Jan 3 at 17:12






  • 1





    You cannot return to your function from within a callback.

    – creativecreatorormaybenot
    Jan 3 at 17:13











  • If B() returns the increased i, would that solve the problem?

    – George
    Jan 3 at 17:15











  • stackoverflow.com/questions/14220321/… I think you need a promise

    – sferret
    Jan 3 at 17:15











  • Which is it? Do you want to wait for B to return or for the button to be pressed? Those are very different things.

    – melpomene
    Jan 3 at 17:17








10




10





B() does not return a promise. It actually doesn't return anything

– Taplar
Jan 3 at 17:12





B() does not return a promise. It actually doesn't return anything

– Taplar
Jan 3 at 17:12




1




1





You cannot return to your function from within a callback.

– creativecreatorormaybenot
Jan 3 at 17:13





You cannot return to your function from within a callback.

– creativecreatorormaybenot
Jan 3 at 17:13













If B() returns the increased i, would that solve the problem?

– George
Jan 3 at 17:15





If B() returns the increased i, would that solve the problem?

– George
Jan 3 at 17:15













stackoverflow.com/questions/14220321/… I think you need a promise

– sferret
Jan 3 at 17:15





stackoverflow.com/questions/14220321/… I think you need a promise

– sferret
Jan 3 at 17:15













Which is it? Do you want to wait for B to return or for the button to be pressed? Those are very different things.

– melpomene
Jan 3 at 17:17





Which is it? Do you want to wait for B to return or for the button to be pressed? Those are very different things.

– melpomene
Jan 3 at 17:17












2 Answers
2






active

oldest

votes


















0














1. Number is passed by value



Thus, giving i to nextI increases a local copy (i.e. the input parameter).



2. B doesn't return Promise



function B(x, y) {
let button = document.getElementById("button");

// FIX HERE
return new Promise(resolve =>
button.addEventListener("click", () => {
console.log("Button clicked.");
resolve('some data')
})
)
}


3. Promise#then accepts a function



So, B (nextI (i)) isn't correct, but B(() => nextI (i)).




N. Other issues



There should be other issues there. For example, you're running functions whose return type/value is Promise in a fire&forget way. Take a look at Promise.all or Promise.race.






share|improve this answer































    0














    before using .then(), you first have to return a promise.



    function B(x, y){
    return new Promise((resolve, reject) => {
    // do something here

    let button = document.getElementById("button");
    button.addEventListener("click", () => {
    console.log("Button clicked.");
    resolve('foo');
    });

    });
    }


    take a look https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise






    share|improve this answer


























    • They want to return data from within the click callback and it might be necessary to explain how to do that.

      – creativecreatorormaybenot
      Jan 3 at 17:19






    • 1





      they can use in the promise --- let button = document.getElementById("button"); button.addEventListener("click", () => { console.log("Button clicked."); resolve('foo'); });

      – Vinay Sheoran
      Jan 3 at 17:21











    • we are resolving promise when the button is clicked, so .then() will be executed after the button is clicked.

      – Vinay Sheoran
      Jan 3 at 17:22


















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    1. Number is passed by value



    Thus, giving i to nextI increases a local copy (i.e. the input parameter).



    2. B doesn't return Promise



    function B(x, y) {
    let button = document.getElementById("button");

    // FIX HERE
    return new Promise(resolve =>
    button.addEventListener("click", () => {
    console.log("Button clicked.");
    resolve('some data')
    })
    )
    }


    3. Promise#then accepts a function



    So, B (nextI (i)) isn't correct, but B(() => nextI (i)).




    N. Other issues



    There should be other issues there. For example, you're running functions whose return type/value is Promise in a fire&forget way. Take a look at Promise.all or Promise.race.






    share|improve this answer




























      0














      1. Number is passed by value



      Thus, giving i to nextI increases a local copy (i.e. the input parameter).



      2. B doesn't return Promise



      function B(x, y) {
      let button = document.getElementById("button");

      // FIX HERE
      return new Promise(resolve =>
      button.addEventListener("click", () => {
      console.log("Button clicked.");
      resolve('some data')
      })
      )
      }


      3. Promise#then accepts a function



      So, B (nextI (i)) isn't correct, but B(() => nextI (i)).




      N. Other issues



      There should be other issues there. For example, you're running functions whose return type/value is Promise in a fire&forget way. Take a look at Promise.all or Promise.race.






      share|improve this answer


























        0












        0








        0







        1. Number is passed by value



        Thus, giving i to nextI increases a local copy (i.e. the input parameter).



        2. B doesn't return Promise



        function B(x, y) {
        let button = document.getElementById("button");

        // FIX HERE
        return new Promise(resolve =>
        button.addEventListener("click", () => {
        console.log("Button clicked.");
        resolve('some data')
        })
        )
        }


        3. Promise#then accepts a function



        So, B (nextI (i)) isn't correct, but B(() => nextI (i)).




        N. Other issues



        There should be other issues there. For example, you're running functions whose return type/value is Promise in a fire&forget way. Take a look at Promise.all or Promise.race.






        share|improve this answer













        1. Number is passed by value



        Thus, giving i to nextI increases a local copy (i.e. the input parameter).



        2. B doesn't return Promise



        function B(x, y) {
        let button = document.getElementById("button");

        // FIX HERE
        return new Promise(resolve =>
        button.addEventListener("click", () => {
        console.log("Button clicked.");
        resolve('some data')
        })
        )
        }


        3. Promise#then accepts a function



        So, B (nextI (i)) isn't correct, but B(() => nextI (i)).




        N. Other issues



        There should be other issues there. For example, you're running functions whose return type/value is Promise in a fire&forget way. Take a look at Promise.all or Promise.race.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 17:24









        Matías FidemraizerMatías Fidemraizer

        49.7k1285148




        49.7k1285148

























            0














            before using .then(), you first have to return a promise.



            function B(x, y){
            return new Promise((resolve, reject) => {
            // do something here

            let button = document.getElementById("button");
            button.addEventListener("click", () => {
            console.log("Button clicked.");
            resolve('foo');
            });

            });
            }


            take a look https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise






            share|improve this answer


























            • They want to return data from within the click callback and it might be necessary to explain how to do that.

              – creativecreatorormaybenot
              Jan 3 at 17:19






            • 1





              they can use in the promise --- let button = document.getElementById("button"); button.addEventListener("click", () => { console.log("Button clicked."); resolve('foo'); });

              – Vinay Sheoran
              Jan 3 at 17:21











            • we are resolving promise when the button is clicked, so .then() will be executed after the button is clicked.

              – Vinay Sheoran
              Jan 3 at 17:22
















            0














            before using .then(), you first have to return a promise.



            function B(x, y){
            return new Promise((resolve, reject) => {
            // do something here

            let button = document.getElementById("button");
            button.addEventListener("click", () => {
            console.log("Button clicked.");
            resolve('foo');
            });

            });
            }


            take a look https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise






            share|improve this answer


























            • They want to return data from within the click callback and it might be necessary to explain how to do that.

              – creativecreatorormaybenot
              Jan 3 at 17:19






            • 1





              they can use in the promise --- let button = document.getElementById("button"); button.addEventListener("click", () => { console.log("Button clicked."); resolve('foo'); });

              – Vinay Sheoran
              Jan 3 at 17:21











            • we are resolving promise when the button is clicked, so .then() will be executed after the button is clicked.

              – Vinay Sheoran
              Jan 3 at 17:22














            0












            0








            0







            before using .then(), you first have to return a promise.



            function B(x, y){
            return new Promise((resolve, reject) => {
            // do something here

            let button = document.getElementById("button");
            button.addEventListener("click", () => {
            console.log("Button clicked.");
            resolve('foo');
            });

            });
            }


            take a look https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise






            share|improve this answer















            before using .then(), you first have to return a promise.



            function B(x, y){
            return new Promise((resolve, reject) => {
            // do something here

            let button = document.getElementById("button");
            button.addEventListener("click", () => {
            console.log("Button clicked.");
            resolve('foo');
            });

            });
            }


            take a look https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 3 at 17:26

























            answered Jan 3 at 17:18









            Vinay SheoranVinay Sheoran

            31339




            31339













            • They want to return data from within the click callback and it might be necessary to explain how to do that.

              – creativecreatorormaybenot
              Jan 3 at 17:19






            • 1





              they can use in the promise --- let button = document.getElementById("button"); button.addEventListener("click", () => { console.log("Button clicked."); resolve('foo'); });

              – Vinay Sheoran
              Jan 3 at 17:21











            • we are resolving promise when the button is clicked, so .then() will be executed after the button is clicked.

              – Vinay Sheoran
              Jan 3 at 17:22



















            • They want to return data from within the click callback and it might be necessary to explain how to do that.

              – creativecreatorormaybenot
              Jan 3 at 17:19






            • 1





              they can use in the promise --- let button = document.getElementById("button"); button.addEventListener("click", () => { console.log("Button clicked."); resolve('foo'); });

              – Vinay Sheoran
              Jan 3 at 17:21











            • we are resolving promise when the button is clicked, so .then() will be executed after the button is clicked.

              – Vinay Sheoran
              Jan 3 at 17:22

















            They want to return data from within the click callback and it might be necessary to explain how to do that.

            – creativecreatorormaybenot
            Jan 3 at 17:19





            They want to return data from within the click callback and it might be necessary to explain how to do that.

            – creativecreatorormaybenot
            Jan 3 at 17:19




            1




            1





            they can use in the promise --- let button = document.getElementById("button"); button.addEventListener("click", () => { console.log("Button clicked."); resolve('foo'); });

            – Vinay Sheoran
            Jan 3 at 17:21





            they can use in the promise --- let button = document.getElementById("button"); button.addEventListener("click", () => { console.log("Button clicked."); resolve('foo'); });

            – Vinay Sheoran
            Jan 3 at 17:21













            we are resolving promise when the button is clicked, so .then() will be executed after the button is clicked.

            – Vinay Sheoran
            Jan 3 at 17:22





            we are resolving promise when the button is clicked, so .then() will be executed after the button is clicked.

            – Vinay Sheoran
            Jan 3 at 17:22



            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas