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))javascript node.js promise
add a comment |
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))javascript node.js promise
add a comment |
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))javascript node.js promise
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
javascript node.js promise
edited Nov 10 at 9:03
CertainPerformance
69.6k143453
69.6k143453
asked Nov 10 at 8:55
xx yy
286
286
add a comment |
add a comment |
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))
1
I suppose it should bethis.theninstead ofsuper.then, also unclear what's the purpose ofdonesince it's same thing asthen.
– 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
add a comment |
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))
1
I suppose it should bethis.theninstead ofsuper.then, also unclear what's the purpose ofdonesince it's same thing asthen.
– 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
add a comment |
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))
1
I suppose it should bethis.theninstead ofsuper.then, also unclear what's the purpose ofdonesince it's same thing asthen.
– 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
add a comment |
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))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))answered Nov 10 at 8:59
CertainPerformance
69.6k143453
69.6k143453
1
I suppose it should bethis.theninstead ofsuper.then, also unclear what's the purpose ofdonesince it's same thing asthen.
– 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
add a comment |
1
I suppose it should bethis.theninstead ofsuper.then, also unclear what's the purpose ofdonesince it's same thing asthen.
– 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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