New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make timeout clearable #15
Conversation
This will need some tests. |
I tried but couldn't mock clearTimeout global method, using sinon and fakeTimers to be able to test like with haveBeenCalled. I have searched ava main repo also and found this suggestion but couldn't apply it. And also tried to ninos by jamie, and also couldn't apply it. Do you have any suggestion to mock clearTimeout like jest timer mocks ? |
hi @sindresorhus, i am sorry for bothering you, is there anything wrong with the code that i can fix. |
This will need doc updates to the readme and updates to index.d.ts. |
test.js
Outdated
@@ -55,3 +55,10 @@ test('calls `.cancel()` on promise when it exists', async t => { | |||
await t.throwsAsync(pTimeout(promise, 50), pTimeout.TimeoutError); | |||
t.true(promise.isCanceled); | |||
}); | |||
|
|||
test('clears timeout when clear triggered', t => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be better written.
index.js
Outdated
} catch (error) { | ||
reject(error); | ||
} | ||
const p = new Promise((resolve, reject) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use descriptive variable names
test.js
Outdated
|
||
test('clears timeout when clear triggered', t => { | ||
const pT = pTimeout(delay(200), 50); | ||
pT.clear(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use descriptive variable names
As for testing, you can just check the time the promise took to resolve. See: https://github.com/sindresorhus/p-map/blob/master/test.js |
The PR needs a better title. It succinctly should describe what's being implemented. |
hi @sindresorhus, I have tried my best to update the code with your specifications. But I didn't understand why test failed. It runs well on my machine windows 10. |
Try to do some debugging yourself. Do some console logging of what the value is for the failing test and then you can use that info to further look into why it's failing on Travis. |
I have tried to debug, and re-run the job from forked repository, but didn't get any error. here is the logs: https://travis-ci.com/github/ilkerceng/p-timeout/jobs/369961763 |
You still need to update the readme and add a doc comment to the clear method in index.d.ts. |
index.d.ts
Outdated
@@ -40,7 +40,7 @@ declare const pTimeout: { | |||
): ClearablePromise<ValueType>; | |||
|
|||
/** | |||
Timeout a promise after a specified amount of time. | |||
Timeout a promise after a specified amount of time. It returns a decorated promise having `.clear()` method to be able to clear timeout. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not in the same location as the readme. I think it could also be better written.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I refactored the docs. But really don't know how it could be better :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You didn't fix it in both places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This addition should be removed.
index.d.ts
Outdated
|
||
If you pass in a cancelable promise, specifically a promise with a `.cancel()` method, that method will be called when the `pTimeout` promise times out. | ||
|
||
@param input - Promise to decorate. | ||
@param milliseconds - Milliseconds before timing out. | ||
@param message - Specify a custom error message or error. If you do a custom error, it's recommended to sub-class `pTimeout.TimeoutError`. Default: `'Promise timed out after 50 milliseconds'`. | ||
@returns A decorated `input` that times out after `milliseconds` time. | ||
@returns A decorated `input` that times out after `milliseconds` time. Decorated input has a `.clear()` method, that method can be used to clear timeout. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@returns A decorated `input` that times out after `milliseconds` time. Decorated input has a `.clear()` method, that method can be used to clear timeout. | |
@returns A decorated `input` that times out after `milliseconds` time. It has a `.clear()` method that clears the timeout. |
index.d.ts
Outdated
@@ -7,6 +7,10 @@ declare namespace pTimeout { | |||
type TimeoutError = TimeoutErrorClass; | |||
} | |||
|
|||
interface ClearablePromise<T> extends Promise<T>{ | |||
clear: () => void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs a short doc-comment.
index.d.ts
Outdated
@@ -7,16 +7,23 @@ declare namespace pTimeout { | |||
type TimeoutError = TimeoutErrorClass; | |||
} | |||
|
|||
/** | |||
* Promise with `.clear() method |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not useful. It just describes what the types already clearly define. The doc comment should describe what it does and its purpose for humans.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's also in the incorrect place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, what about Promise with .clear() method that clears the timeout.
?
And i didn't understand why it is in the incorrect place. It is just on top of interface declaration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doc comment should be placed above the clear: ()
and should describe what **.clear()
does.
@ilkerceng Bump |
…ction from outside
@sindresorhus bump :) |
Hi @sindresorhus, it gives these errors but they are not in my committed codes. I think, existing codebase v4 gives these errors. Error: /home/runner/work/p-timeout/p-timeout/index.test-d.ts: line 1, col 1, Error - AVA ignores this file. (ava/no-ignored-test-files) |
Hi @sindresorhus, i think it is ok now |
Do i miss any review request in my code? If any, i can fix it. |
I'm just a bit burned out by this PR. I've already spent more than 10x the time to review this than it would have taken me to implement it myself. In the future, try to spend more time on a pull request before submitting it. |
index.d.ts
Outdated
@@ -93,14 +100,15 @@ declare const pTimeout: { | |||
pTimeout(delayedPromise(), 50, () => { | |||
return pTimeout(delayedPromise(), 300); | |||
}); | |||
promise.clear(); //Clear timeout |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not relevant to the current example, because it makes some of the code moot here. Please remove.
index.d.ts
Outdated
@@ -40,7 +40,7 @@ declare const pTimeout: { | |||
): ClearablePromise<ValueType>; | |||
|
|||
/** | |||
Timeout a promise after a specified amount of time. | |||
Timeout a promise after a specified amount of time. It returns a decorated promise having `.clear()` method to be able to clear timeout. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This addition should be removed.
index.d.ts
Outdated
/** | ||
* .clear() method that clears the timeout. | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/** | |
* .clear() method that clears the timeout. | |
*/ | |
/** | |
Clear the timeout. | |
*/ |
You are absolutely right. I'm really sorry. |
Hi, i just tried to fix #6