Node.js Promise setTimeout resolves quicker than expectedHow do I resolve “Cannot find module” error using Node.js?Is the recommendation to include CSS before JavaScript invalid?Bluebird-Queue Concurrency in Promise Queues not working as expectedHow do you wrap setTimeout in a promiseMonitoring pending async operations in Node.js promised environmentHow to resolve promises in order?wait for sinon stubbed promise to resolve before making assertion on sinon spyNODE.js Error during test file unhandled promiseJS Promise starts next promise in the chain before the previous one resolvesPromises yielded from a generator not resolved concurrently when awaiting
Why is indicated airspeed rather than ground speed used during the takeoff roll?
Reasons for having MCU pin-states default to pull-up/down out of reset
If the Dominion rule using their Jem'Hadar troops, why is their life expectancy so low?
New Order #2: Turn My Way
What is the probability that the nth card becomes the top card after shuffling a certain way?
Should I warn a new PhD Student?
What is it called when someone votes for an option that's not their first choice?
Do people actually use the word "kaputt" in conversation?
Walter Rudin's mathematical analysis: theorem 2.43. Why proof can't work under the perfect set is uncountable.
Reason why a kingside attack is not justified
Did I make a mistake by ccing email to boss to others?
categorizing a variable turns it from insignificant to significant
I keep switching characters, how do I stop?
Why is implicit conversion not ambiguous for non-primitive types?
How to track Account Description field changes in Field history Tracking?
Extract substring according to regexp with sed or grep
Not hide and seek
"Marked down as someone wanting to sell shares." What does that mean?
How do you say "Trust your struggle." in French?
Mortal danger in mid-grade literature
Should I be concerned about student access to a test bank?
What properties make a magic weapon befit a Rogue more than a DEX-based Fighter?
What is the tangent at a sharp point on a curve?
Why would five hundred and five same as one?
Node.js Promise setTimeout resolves quicker than expected
How do I resolve “Cannot find module” error using Node.js?Is the recommendation to include CSS before JavaScript invalid?Bluebird-Queue Concurrency in Promise Queues not working as expectedHow do you wrap setTimeout in a promiseMonitoring pending async operations in Node.js promised environmentHow to resolve promises in order?wait for sinon stubbed promise to resolve before making assertion on sinon spyNODE.js Error during test file unhandled promiseJS Promise starts next promise in the chain before the previous one resolvesPromises yielded from a generator not resolved concurrently when awaiting
I have the following code in Node.js.
const timeout = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
I'm trying to test this code with the following test:
it("Should wait for given time before resolving", async () =>
const MS = 100;
const start = process.hrtime();
await timeout(MS);
const diff = process.hrtime(start);
expect(((diff[0] * NS_PER_SEC) + diff[1]) / 1000000).to.at.least(MS);
);
The problem is sometimes (rarely), this test fails:
Should wait for given time before resolving:
AssertionError: expected 99.595337 to be at least 100
+ expected - actual
-99.595337
+100
Obviously this is some type of timing issue with Node.js or something. If anything I expect await timeout(MS);
to take slightly longer than MS
. In no case do I expect it to take less time.
What is it about the internals of JavaScript/Node.js that causes this to happen?
This occurred on macOS 10.14.3 running Node.js version 8.15.1.
javascript node.js
|
show 6 more comments
I have the following code in Node.js.
const timeout = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
I'm trying to test this code with the following test:
it("Should wait for given time before resolving", async () =>
const MS = 100;
const start = process.hrtime();
await timeout(MS);
const diff = process.hrtime(start);
expect(((diff[0] * NS_PER_SEC) + diff[1]) / 1000000).to.at.least(MS);
);
The problem is sometimes (rarely), this test fails:
Should wait for given time before resolving:
AssertionError: expected 99.595337 to be at least 100
+ expected - actual
-99.595337
+100
Obviously this is some type of timing issue with Node.js or something. If anything I expect await timeout(MS);
to take slightly longer than MS
. In no case do I expect it to take less time.
What is it about the internals of JavaScript/Node.js that causes this to happen?
This occurred on macOS 10.14.3 running Node.js version 8.15.1.
javascript node.js
2
Is there a reason you're testing this? I can see testing to make sure thattimeout
creates a promise and calls a stubbed outsetTimeout
with the correct params, but it's not your responsibility to ensure the behavior of a core feature like setTimeout.
– Nicholas Tower
Mar 7 at 1:33
@NicholasTower It's still logic I'm writing. It would be a dumb mistake, but if I ever were to putms / 2
instead of justms
or not actually callresolve
, I'd want to try to have my tests catch it. Obviously those are dumb situations, but to me, the more tests the better. Even if it is irrelevant, I think the question itself is still relevant. How do I knowsetTimeout
is really working if I'm getting this inconsistent behavior? So in some regard the test proved at least a bit useful and is causing me to dig deeper. Which IMO is valuable.
– Charlie Fish
Mar 7 at 1:44
What OS are you running the test on? Perhaps the underlying syscall is to blame: frenchfries.net/paul/dfly/nanosleep.html
– ııı
Mar 7 at 1:45
1
FWIW too, you can see it at work here, even withDate
objects (although these tend to be rounded to 100 in some very-close-to-100 cases). It also works here in the browser - anyway on my Firefox - using onlyDate
objects. No idea why it behaves like this though.
– Stock Overflaw
Mar 7 at 3:41
1
It's odd that it's running short. The guarantee is that the timeout will run at or after the set time. The amount of time after is ASAP but could be a long time depending on what else is happening. What version are you using? There has been at least one bug like this insetTimeout
but these are pretty old.
– Ouroborus
Mar 7 at 4:35
|
show 6 more comments
I have the following code in Node.js.
const timeout = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
I'm trying to test this code with the following test:
it("Should wait for given time before resolving", async () =>
const MS = 100;
const start = process.hrtime();
await timeout(MS);
const diff = process.hrtime(start);
expect(((diff[0] * NS_PER_SEC) + diff[1]) / 1000000).to.at.least(MS);
);
The problem is sometimes (rarely), this test fails:
Should wait for given time before resolving:
AssertionError: expected 99.595337 to be at least 100
+ expected - actual
-99.595337
+100
Obviously this is some type of timing issue with Node.js or something. If anything I expect await timeout(MS);
to take slightly longer than MS
. In no case do I expect it to take less time.
What is it about the internals of JavaScript/Node.js that causes this to happen?
This occurred on macOS 10.14.3 running Node.js version 8.15.1.
javascript node.js
I have the following code in Node.js.
const timeout = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
I'm trying to test this code with the following test:
it("Should wait for given time before resolving", async () =>
const MS = 100;
const start = process.hrtime();
await timeout(MS);
const diff = process.hrtime(start);
expect(((diff[0] * NS_PER_SEC) + diff[1]) / 1000000).to.at.least(MS);
);
The problem is sometimes (rarely), this test fails:
Should wait for given time before resolving:
AssertionError: expected 99.595337 to be at least 100
+ expected - actual
-99.595337
+100
Obviously this is some type of timing issue with Node.js or something. If anything I expect await timeout(MS);
to take slightly longer than MS
. In no case do I expect it to take less time.
What is it about the internals of JavaScript/Node.js that causes this to happen?
This occurred on macOS 10.14.3 running Node.js version 8.15.1.
javascript node.js
javascript node.js
edited Mar 7 at 4:36
Charlie Fish
asked Mar 7 at 1:29
Charlie FishCharlie Fish
5,72563177
5,72563177
2
Is there a reason you're testing this? I can see testing to make sure thattimeout
creates a promise and calls a stubbed outsetTimeout
with the correct params, but it's not your responsibility to ensure the behavior of a core feature like setTimeout.
– Nicholas Tower
Mar 7 at 1:33
@NicholasTower It's still logic I'm writing. It would be a dumb mistake, but if I ever were to putms / 2
instead of justms
or not actually callresolve
, I'd want to try to have my tests catch it. Obviously those are dumb situations, but to me, the more tests the better. Even if it is irrelevant, I think the question itself is still relevant. How do I knowsetTimeout
is really working if I'm getting this inconsistent behavior? So in some regard the test proved at least a bit useful and is causing me to dig deeper. Which IMO is valuable.
– Charlie Fish
Mar 7 at 1:44
What OS are you running the test on? Perhaps the underlying syscall is to blame: frenchfries.net/paul/dfly/nanosleep.html
– ııı
Mar 7 at 1:45
1
FWIW too, you can see it at work here, even withDate
objects (although these tend to be rounded to 100 in some very-close-to-100 cases). It also works here in the browser - anyway on my Firefox - using onlyDate
objects. No idea why it behaves like this though.
– Stock Overflaw
Mar 7 at 3:41
1
It's odd that it's running short. The guarantee is that the timeout will run at or after the set time. The amount of time after is ASAP but could be a long time depending on what else is happening. What version are you using? There has been at least one bug like this insetTimeout
but these are pretty old.
– Ouroborus
Mar 7 at 4:35
|
show 6 more comments
2
Is there a reason you're testing this? I can see testing to make sure thattimeout
creates a promise and calls a stubbed outsetTimeout
with the correct params, but it's not your responsibility to ensure the behavior of a core feature like setTimeout.
– Nicholas Tower
Mar 7 at 1:33
@NicholasTower It's still logic I'm writing. It would be a dumb mistake, but if I ever were to putms / 2
instead of justms
or not actually callresolve
, I'd want to try to have my tests catch it. Obviously those are dumb situations, but to me, the more tests the better. Even if it is irrelevant, I think the question itself is still relevant. How do I knowsetTimeout
is really working if I'm getting this inconsistent behavior? So in some regard the test proved at least a bit useful and is causing me to dig deeper. Which IMO is valuable.
– Charlie Fish
Mar 7 at 1:44
What OS are you running the test on? Perhaps the underlying syscall is to blame: frenchfries.net/paul/dfly/nanosleep.html
– ııı
Mar 7 at 1:45
1
FWIW too, you can see it at work here, even withDate
objects (although these tend to be rounded to 100 in some very-close-to-100 cases). It also works here in the browser - anyway on my Firefox - using onlyDate
objects. No idea why it behaves like this though.
– Stock Overflaw
Mar 7 at 3:41
1
It's odd that it's running short. The guarantee is that the timeout will run at or after the set time. The amount of time after is ASAP but could be a long time depending on what else is happening. What version are you using? There has been at least one bug like this insetTimeout
but these are pretty old.
– Ouroborus
Mar 7 at 4:35
2
2
Is there a reason you're testing this? I can see testing to make sure that
timeout
creates a promise and calls a stubbed out setTimeout
with the correct params, but it's not your responsibility to ensure the behavior of a core feature like setTimeout.– Nicholas Tower
Mar 7 at 1:33
Is there a reason you're testing this? I can see testing to make sure that
timeout
creates a promise and calls a stubbed out setTimeout
with the correct params, but it's not your responsibility to ensure the behavior of a core feature like setTimeout.– Nicholas Tower
Mar 7 at 1:33
@NicholasTower It's still logic I'm writing. It would be a dumb mistake, but if I ever were to put
ms / 2
instead of just ms
or not actually call resolve
, I'd want to try to have my tests catch it. Obviously those are dumb situations, but to me, the more tests the better. Even if it is irrelevant, I think the question itself is still relevant. How do I know setTimeout
is really working if I'm getting this inconsistent behavior? So in some regard the test proved at least a bit useful and is causing me to dig deeper. Which IMO is valuable.– Charlie Fish
Mar 7 at 1:44
@NicholasTower It's still logic I'm writing. It would be a dumb mistake, but if I ever were to put
ms / 2
instead of just ms
or not actually call resolve
, I'd want to try to have my tests catch it. Obviously those are dumb situations, but to me, the more tests the better. Even if it is irrelevant, I think the question itself is still relevant. How do I know setTimeout
is really working if I'm getting this inconsistent behavior? So in some regard the test proved at least a bit useful and is causing me to dig deeper. Which IMO is valuable.– Charlie Fish
Mar 7 at 1:44
What OS are you running the test on? Perhaps the underlying syscall is to blame: frenchfries.net/paul/dfly/nanosleep.html
– ııı
Mar 7 at 1:45
What OS are you running the test on? Perhaps the underlying syscall is to blame: frenchfries.net/paul/dfly/nanosleep.html
– ııı
Mar 7 at 1:45
1
1
FWIW too, you can see it at work here, even with
Date
objects (although these tend to be rounded to 100 in some very-close-to-100 cases). It also works here in the browser - anyway on my Firefox - using only Date
objects. No idea why it behaves like this though.– Stock Overflaw
Mar 7 at 3:41
FWIW too, you can see it at work here, even with
Date
objects (although these tend to be rounded to 100 in some very-close-to-100 cases). It also works here in the browser - anyway on my Firefox - using only Date
objects. No idea why it behaves like this though.– Stock Overflaw
Mar 7 at 3:41
1
1
It's odd that it's running short. The guarantee is that the timeout will run at or after the set time. The amount of time after is ASAP but could be a long time depending on what else is happening. What version are you using? There has been at least one bug like this in
setTimeout
but these are pretty old.– Ouroborus
Mar 7 at 4:35
It's odd that it's running short. The guarantee is that the timeout will run at or after the set time. The amount of time after is ASAP but could be a long time depending on what else is happening. What version are you using? There has been at least one bug like this in
setTimeout
but these are pretty old.– Ouroborus
Mar 7 at 4:35
|
show 6 more comments
0
active
oldest
votes
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
);
);
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%2f55034697%2fnode-js-promise-settimeout-resolves-quicker-than-expected%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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%2f55034697%2fnode-js-promise-settimeout-resolves-quicker-than-expected%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
2
Is there a reason you're testing this? I can see testing to make sure that
timeout
creates a promise and calls a stubbed outsetTimeout
with the correct params, but it's not your responsibility to ensure the behavior of a core feature like setTimeout.– Nicholas Tower
Mar 7 at 1:33
@NicholasTower It's still logic I'm writing. It would be a dumb mistake, but if I ever were to put
ms / 2
instead of justms
or not actually callresolve
, I'd want to try to have my tests catch it. Obviously those are dumb situations, but to me, the more tests the better. Even if it is irrelevant, I think the question itself is still relevant. How do I knowsetTimeout
is really working if I'm getting this inconsistent behavior? So in some regard the test proved at least a bit useful and is causing me to dig deeper. Which IMO is valuable.– Charlie Fish
Mar 7 at 1:44
What OS are you running the test on? Perhaps the underlying syscall is to blame: frenchfries.net/paul/dfly/nanosleep.html
– ııı
Mar 7 at 1:45
1
FWIW too, you can see it at work here, even with
Date
objects (although these tend to be rounded to 100 in some very-close-to-100 cases). It also works here in the browser - anyway on my Firefox - using onlyDate
objects. No idea why it behaves like this though.– Stock Overflaw
Mar 7 at 3:41
1
It's odd that it's running short. The guarantee is that the timeout will run at or after the set time. The amount of time after is ASAP but could be a long time depending on what else is happening. What version are you using? There has been at least one bug like this in
setTimeout
but these are pretty old.– Ouroborus
Mar 7 at 4:35