javascript promise: a problem with setTimeout inside a promise race









up vote
1
down vote

favorite












the following code outputs (1) then (3) whitch is wrong .. why?



the following code is intended to wait 3 seconds and print (3) then wait another 1 second then print (1)
so the correct order must be (3) then (1)



I guess the problem is wait() returns a new promise, but how can I fix this issue
note: all functions must return 'this' for chaining






class test extends Promise 
constructor(fn)
super(fn)
return this


wait(seconds)
return new test(resolve =>
setTimeout(function()
resolve(seconds)
, seconds * 1000)
)


done(fn)
return super.then(fn)



p = new test(r => r())
p.wait(3) //(2) must be before (1)
.done(x => console.log(x))
.wait(1)
.done(x => console.log(x))












share|improve this question



























    up vote
    1
    down vote

    favorite












    the following code outputs (1) then (3) whitch is wrong .. why?



    the following code is intended to wait 3 seconds and print (3) then wait another 1 second then print (1)
    so the correct order must be (3) then (1)



    I guess the problem is wait() returns a new promise, but how can I fix this issue
    note: all functions must return 'this' for chaining






    class test extends Promise 
    constructor(fn)
    super(fn)
    return this


    wait(seconds)
    return new test(resolve =>
    setTimeout(function()
    resolve(seconds)
    , seconds * 1000)
    )


    done(fn)
    return super.then(fn)



    p = new test(r => r())
    p.wait(3) //(2) must be before (1)
    .done(x => console.log(x))
    .wait(1)
    .done(x => console.log(x))












    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      the following code outputs (1) then (3) whitch is wrong .. why?



      the following code is intended to wait 3 seconds and print (3) then wait another 1 second then print (1)
      so the correct order must be (3) then (1)



      I guess the problem is wait() returns a new promise, but how can I fix this issue
      note: all functions must return 'this' for chaining






      class test extends Promise 
      constructor(fn)
      super(fn)
      return this


      wait(seconds)
      return new test(resolve =>
      setTimeout(function()
      resolve(seconds)
      , seconds * 1000)
      )


      done(fn)
      return super.then(fn)



      p = new test(r => r())
      p.wait(3) //(2) must be before (1)
      .done(x => console.log(x))
      .wait(1)
      .done(x => console.log(x))












      share|improve this question















      the following code outputs (1) then (3) whitch is wrong .. why?



      the following code is intended to wait 3 seconds and print (3) then wait another 1 second then print (1)
      so the correct order must be (3) then (1)



      I guess the problem is wait() returns a new promise, but how can I fix this issue
      note: all functions must return 'this' for chaining






      class test extends Promise 
      constructor(fn)
      super(fn)
      return this


      wait(seconds)
      return new test(resolve =>
      setTimeout(function()
      resolve(seconds)
      , seconds * 1000)
      )


      done(fn)
      return super.then(fn)



      p = new test(r => r())
      p.wait(3) //(2) must be before (1)
      .done(x => console.log(x))
      .wait(1)
      .done(x => console.log(x))








      class test extends Promise 
      constructor(fn)
      super(fn)
      return this


      wait(seconds)
      return new test(resolve =>
      setTimeout(function()
      resolve(seconds)
      , seconds * 1000)
      )


      done(fn)
      return super.then(fn)



      p = new test(r => r())
      p.wait(3) //(2) must be before (1)
      .done(x => console.log(x))
      .wait(1)
      .done(x => console.log(x))





      class test extends Promise 
      constructor(fn)
      super(fn)
      return this


      wait(seconds)
      return new test(resolve =>
      setTimeout(function()
      resolve(seconds)
      , seconds * 1000)
      )


      done(fn)
      return super.then(fn)



      p = new test(r => r())
      p.wait(3) //(2) must be before (1)
      .done(x => console.log(x))
      .wait(1)
      .done(x => console.log(x))






      javascript node.js promise






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 9:03









      CertainPerformance

      69.6k143453




      69.6k143453










      asked Nov 10 at 8:55









      xx yy

      286




      286






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          You need wait to call .then on the current test object (that is, this), and return the constructed Promise chain:






          class test extends Promise 
          constructor(fn)
          super(fn)
          return this


          wait(seconds)
          return this.then(() => new test(resolve =>
          setTimeout(function()
          resolve(seconds)
          , seconds * 1000);
          ))


          done(fn)
          return super.then(fn)



          console.log('start');
          p = new test(r => r())
          p.wait(3) //(2) must be before (1)
          .done(x => console.log(x))
          .wait(1)
          .done(x => console.log(x))








          share|improve this answer
















          • 1




            I suppose it should be this.then instead of super.then, also unclear what's the purpose of done since it's same thing as then.
            – estus
            Nov 10 at 9:06










          • worked! ..thank you @CertainPerformance , you also guide me to a good lesson that I have to use .then() to wait for the previous function in the chain
            – xx yy
            Nov 10 at 9:32










          • I have a method this.then() with a different signature witch calls super.then(), if I used this.then() inside it, it will lead to infinite loop , do you have another idea? @estus
            – xx yy
            Nov 10 at 9:34










          • @xxyy This isn't shown in the question, so it depends. It's not evident that you need to extend Promise at all since your class doesn't use promise API but its own. You could make use of en.wikipedia.org/wiki/Composition_over_inheritance and use promises internally.
            – estus
            Nov 10 at 9:50










          • class myPromise extends Promise then(success,fail,otherArgument) if(otherArgument)doSomething() return this.then(success,fail)
            – xx yy
            Nov 12 at 7:23











          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',
          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%2f53237418%2fjavascript-promise-a-problem-with-settimeout-inside-a-promise-race%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          You need wait to call .then on the current test object (that is, this), and return the constructed Promise chain:






          class test extends Promise 
          constructor(fn)
          super(fn)
          return this


          wait(seconds)
          return this.then(() => new test(resolve =>
          setTimeout(function()
          resolve(seconds)
          , seconds * 1000);
          ))


          done(fn)
          return super.then(fn)



          console.log('start');
          p = new test(r => r())
          p.wait(3) //(2) must be before (1)
          .done(x => console.log(x))
          .wait(1)
          .done(x => console.log(x))








          share|improve this answer
















          • 1




            I suppose it should be this.then instead of super.then, also unclear what's the purpose of done since it's same thing as then.
            – estus
            Nov 10 at 9:06










          • worked! ..thank you @CertainPerformance , you also guide me to a good lesson that I have to use .then() to wait for the previous function in the chain
            – xx yy
            Nov 10 at 9:32










          • I have a method this.then() with a different signature witch calls super.then(), if I used this.then() inside it, it will lead to infinite loop , do you have another idea? @estus
            – xx yy
            Nov 10 at 9:34










          • @xxyy This isn't shown in the question, so it depends. It's not evident that you need to extend Promise at all since your class doesn't use promise API but its own. You could make use of en.wikipedia.org/wiki/Composition_over_inheritance and use promises internally.
            – estus
            Nov 10 at 9:50










          • class myPromise extends Promise then(success,fail,otherArgument) if(otherArgument)doSomething() return this.then(success,fail)
            – xx yy
            Nov 12 at 7:23















          up vote
          3
          down vote



          accepted










          You need wait to call .then on the current test object (that is, this), and return the constructed Promise chain:






          class test extends Promise 
          constructor(fn)
          super(fn)
          return this


          wait(seconds)
          return this.then(() => new test(resolve =>
          setTimeout(function()
          resolve(seconds)
          , seconds * 1000);
          ))


          done(fn)
          return super.then(fn)



          console.log('start');
          p = new test(r => r())
          p.wait(3) //(2) must be before (1)
          .done(x => console.log(x))
          .wait(1)
          .done(x => console.log(x))








          share|improve this answer
















          • 1




            I suppose it should be this.then instead of super.then, also unclear what's the purpose of done since it's same thing as then.
            – estus
            Nov 10 at 9:06










          • worked! ..thank you @CertainPerformance , you also guide me to a good lesson that I have to use .then() to wait for the previous function in the chain
            – xx yy
            Nov 10 at 9:32










          • I have a method this.then() with a different signature witch calls super.then(), if I used this.then() inside it, it will lead to infinite loop , do you have another idea? @estus
            – xx yy
            Nov 10 at 9:34










          • @xxyy This isn't shown in the question, so it depends. It's not evident that you need to extend Promise at all since your class doesn't use promise API but its own. You could make use of en.wikipedia.org/wiki/Composition_over_inheritance and use promises internally.
            – estus
            Nov 10 at 9:50










          • class myPromise extends Promise then(success,fail,otherArgument) if(otherArgument)doSomething() return this.then(success,fail)
            – xx yy
            Nov 12 at 7:23













          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          You need wait to call .then on the current test object (that is, this), and return the constructed Promise chain:






          class test extends Promise 
          constructor(fn)
          super(fn)
          return this


          wait(seconds)
          return this.then(() => new test(resolve =>
          setTimeout(function()
          resolve(seconds)
          , seconds * 1000);
          ))


          done(fn)
          return super.then(fn)



          console.log('start');
          p = new test(r => r())
          p.wait(3) //(2) must be before (1)
          .done(x => console.log(x))
          .wait(1)
          .done(x => console.log(x))








          share|improve this answer












          You need wait to call .then on the current test object (that is, this), and return the constructed Promise chain:






          class test extends Promise 
          constructor(fn)
          super(fn)
          return this


          wait(seconds)
          return this.then(() => new test(resolve =>
          setTimeout(function()
          resolve(seconds)
          , seconds * 1000);
          ))


          done(fn)
          return super.then(fn)



          console.log('start');
          p = new test(r => r())
          p.wait(3) //(2) must be before (1)
          .done(x => console.log(x))
          .wait(1)
          .done(x => console.log(x))








          class test extends Promise 
          constructor(fn)
          super(fn)
          return this


          wait(seconds)
          return this.then(() => new test(resolve =>
          setTimeout(function()
          resolve(seconds)
          , seconds * 1000);
          ))


          done(fn)
          return super.then(fn)



          console.log('start');
          p = new test(r => r())
          p.wait(3) //(2) must be before (1)
          .done(x => console.log(x))
          .wait(1)
          .done(x => console.log(x))





          class test extends Promise 
          constructor(fn)
          super(fn)
          return this


          wait(seconds)
          return this.then(() => new test(resolve =>
          setTimeout(function()
          resolve(seconds)
          , seconds * 1000);
          ))


          done(fn)
          return super.then(fn)



          console.log('start');
          p = new test(r => r())
          p.wait(3) //(2) must be before (1)
          .done(x => console.log(x))
          .wait(1)
          .done(x => console.log(x))






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 8:59









          CertainPerformance

          69.6k143453




          69.6k143453







          • 1




            I suppose it should be this.then instead of super.then, also unclear what's the purpose of done since it's same thing as then.
            – estus
            Nov 10 at 9:06










          • worked! ..thank you @CertainPerformance , you also guide me to a good lesson that I have to use .then() to wait for the previous function in the chain
            – xx yy
            Nov 10 at 9:32










          • I have a method this.then() with a different signature witch calls super.then(), if I used this.then() inside it, it will lead to infinite loop , do you have another idea? @estus
            – xx yy
            Nov 10 at 9:34










          • @xxyy This isn't shown in the question, so it depends. It's not evident that you need to extend Promise at all since your class doesn't use promise API but its own. You could make use of en.wikipedia.org/wiki/Composition_over_inheritance and use promises internally.
            – estus
            Nov 10 at 9:50










          • class myPromise extends Promise then(success,fail,otherArgument) if(otherArgument)doSomething() return this.then(success,fail)
            – xx yy
            Nov 12 at 7:23













          • 1




            I suppose it should be this.then instead of super.then, also unclear what's the purpose of done since it's same thing as then.
            – estus
            Nov 10 at 9:06










          • worked! ..thank you @CertainPerformance , you also guide me to a good lesson that I have to use .then() to wait for the previous function in the chain
            – xx yy
            Nov 10 at 9:32










          • I have a method this.then() with a different signature witch calls super.then(), if I used this.then() inside it, it will lead to infinite loop , do you have another idea? @estus
            – xx yy
            Nov 10 at 9:34










          • @xxyy This isn't shown in the question, so it depends. It's not evident that you need to extend Promise at all since your class doesn't use promise API but its own. You could make use of en.wikipedia.org/wiki/Composition_over_inheritance and use promises internally.
            – estus
            Nov 10 at 9:50










          • class myPromise extends Promise then(success,fail,otherArgument) if(otherArgument)doSomething() return this.then(success,fail)
            – xx yy
            Nov 12 at 7:23








          1




          1




          I suppose it should be this.then instead of super.then, also unclear what's the purpose of done since it's same thing as then.
          – estus
          Nov 10 at 9:06




          I suppose it should be this.then instead of super.then, also unclear what's the purpose of done since it's same thing as then.
          – estus
          Nov 10 at 9:06












          worked! ..thank you @CertainPerformance , you also guide me to a good lesson that I have to use .then() to wait for the previous function in the chain
          – xx yy
          Nov 10 at 9:32




          worked! ..thank you @CertainPerformance , you also guide me to a good lesson that I have to use .then() to wait for the previous function in the chain
          – xx yy
          Nov 10 at 9:32












          I have a method this.then() with a different signature witch calls super.then(), if I used this.then() inside it, it will lead to infinite loop , do you have another idea? @estus
          – xx yy
          Nov 10 at 9:34




          I have a method this.then() with a different signature witch calls super.then(), if I used this.then() inside it, it will lead to infinite loop , do you have another idea? @estus
          – xx yy
          Nov 10 at 9:34












          @xxyy This isn't shown in the question, so it depends. It's not evident that you need to extend Promise at all since your class doesn't use promise API but its own. You could make use of en.wikipedia.org/wiki/Composition_over_inheritance and use promises internally.
          – estus
          Nov 10 at 9:50




          @xxyy This isn't shown in the question, so it depends. It's not evident that you need to extend Promise at all since your class doesn't use promise API but its own. You could make use of en.wikipedia.org/wiki/Composition_over_inheritance and use promises internally.
          – estus
          Nov 10 at 9:50












          class myPromise extends Promise then(success,fail,otherArgument) if(otherArgument)doSomething() return this.then(success,fail)
          – xx yy
          Nov 12 at 7:23





          class myPromise extends Promise then(success,fail,otherArgument) if(otherArgument)doSomething() return this.then(success,fail)
          – xx yy
          Nov 12 at 7:23


















          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%2f53237418%2fjavascript-promise-a-problem-with-settimeout-inside-a-promise-race%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

          Kleinkühnau

          Makov (Slowakei)

          Deutsches Schauspielhaus