Skip to content
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

Merged
merged 21 commits into from Dec 26, 2020
Merged

Make timeout clearable #15

merged 21 commits into from Dec 26, 2020

Conversation

ilkerceng
Copy link
Contributor

Hi, i just tried to fix #6

@sindresorhus
Copy link
Owner

This will need some tests.

@ilkerceng
Copy link
Contributor Author

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 ?

@ilkerceng
Copy link
Contributor Author

hi @sindresorhus, i am sorry for bothering you, is there anything wrong with the code that i can fix.

@sindresorhus
Copy link
Owner

This will need doc updates to the readme and updates to index.d.ts.

index.js Outdated Show resolved Hide resolved
test.js Outdated Show resolved Hide resolved
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 => {
Copy link
Owner

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) => {
Copy link
Owner

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();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use descriptive variable names

@sindresorhus
Copy link
Owner

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

@sindresorhus
Copy link
Owner

The PR needs a better title. It succinctly should describe what's being implemented.

@ilkerceng ilkerceng changed the title promise decorated with adding clear function to be able to call clearTimeout from outside Make the resulting promise cancelable Aug 5, 2020
@ilkerceng ilkerceng changed the title Make the resulting promise cancelable Make timeout clearable Aug 5, 2020
@ilkerceng
Copy link
Contributor Author

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.

@sindresorhus
Copy link
Owner

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.

@ilkerceng
Copy link
Contributor Author

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

@sindresorhus
Copy link
Owner

You still need to update the readme and add a doc comment to the clear method in index.d.ts.

index.d.ts Outdated Show resolved Hide resolved
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.
Copy link
Owner

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.

Copy link
Contributor Author

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 :)

Copy link
Owner

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.

Copy link
Owner

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.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@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
Copy link
Owner

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
Copy link
Owner

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.

Copy link
Owner

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.

Copy link
Contributor Author

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.

Copy link
Owner

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.

@sindresorhus
Copy link
Owner

@ilkerceng Bump

@ilkerceng
Copy link
Contributor Author

@sindresorhus bump :)

@ilkerceng
Copy link
Contributor Author

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)
Error: /home/runner/work/p-timeout/p-timeout/index.test-d.ts: line 35, col 46, Error - Functions that return promises must be async. (@typescript-eslint/promise-function-async)
Error: /home/runner/work/p-timeout/p-timeout/index.test-d.ts: line 41, col 47, Error - Functions that return promises must be async. (@typescript-eslint/promise-function-async)

@ilkerceng
Copy link
Contributor Author

Hi @sindresorhus, i think it is ok now

@ilkerceng
Copy link
Contributor Author

Do i miss any review request in my code? If any, i can fix it.

@sindresorhus
Copy link
Owner

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
Copy link
Owner

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.
Copy link
Owner

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
Comment on lines 44 to 46
/**
* .clear() method that clears the timeout.
*/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* .clear() method that clears the timeout.
*/
/**
Clear the timeout.
*/

@ilkerceng
Copy link
Contributor Author

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.

You are absolutely right. I'm really sorry.

@sindresorhus sindresorhus merged commit e26f08d into sindresorhus:master Dec 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make the resulting promise cancelable
2 participants