Comments (9)
This is a useful feature and one that I'd love to see merged. We're currently using @jamesst20 's solution with patch-package
from set-interval-async.
from set-interval-async.
Hi @ealmansi
Have you had the chance to look into this feature request? I see the PR #21 was declined, perhaps you had a better idea in mind?
The interface
setIntervalAsync (() => {}, 1000, ...args) // Always after 1000ms
setIntervalAsync (() => {}, [0, 1000], ...args) // First run after 0ms and then 1000ms
looked pretty logical to me.
Waiting for your answer :)
from set-interval-async.
Hi James,
Thanks for creating this issue; I appreciate it.
Like you pointed out, the snippet above slightly breaks the API. In particular, if the first execution takes longer than the interval to execute, the following property is no longer withheld:
Dynamic: If possible, the given function is called once every interval milliseconds. If any execution takes longer than the desired interval, the next execution is delayed until the previous one has finished, and called immediately after this condition is reached.
Also, if the first execution fails, the process would be interrupted entirely.
I think the best way to achieve this would be changing this line in the definition of setIntervalAsync
:
timer.timeouts[id] = setTimeout(
timeoutHandler,
0, // interval,
timer,
handler,
interval,
...args
)
I would be hesitant of adding in this feature if it implies a departure from the traditional setInterval
API, since compatibility is one of the mail goals for the project.
from set-interval-async.
Thanks for getting back to me.
I understand the drawback from moving away from the original API. A possible workaround could be to export an additional method ?
Exemple usage:
import { setIntervalAsync, setIntervalNowAsync, SetIntervalAsyncTimer } from "set-interval-async/dynamic";
What do you think?
from set-interval-async.
Sorry, I have been lazy and hadn't taken the time to check your implementation. I found a way my feature request could be implemented without any breaking API changes.
Example for dynamic.js
Current implementation:
function setIntervalAsync (handler, interval, ...args) {
validateHandler(handler)
validateInterval(interval)
const timer = new SetIntervalAsyncTimer()
const iterationId = 0
timer.timeouts[iterationId] = setTimeout(
timeoutHandler,
interval,
timer,
iterationId,
handler,
interval,
...args
)
return timer
}
becomes
function setIntervalAsync (handler, interval, ...args) {
validateHandler(handler)
validateInterval(interval)
const timer = new SetIntervalAsyncTimer()
const iterationId = 0
const intervals = [interval].flat();
timer.timeouts[iterationId] = setTimeout(
timeoutHandler,
intervals[0],
timer,
iterationId,
handler,
intervals[1] || intervals[0],
...args
)
return timer
}
No broken calls:
setIntervalAsync (() => {}, 1000, ...args) // Always after 1000ms
setIntervalAsync (() => {}, [0, 1000], ...args) // First run after 0ms and then 1000ms
@ealmansi What do you think?
from set-interval-async.
I also find the API in PR #21 quite good design actually. No breaking change and pretty straightforward. Did you find another solution @james-em or @jamesst20 for this feature ? Another package maybe ?
Thank you for your help
from set-interval-async.
lazy and hadn't taken the time to check your implementation. I found a way my feature request could be implemented without any breaking API changes.
Example for dynamic.js
I still use the same implementation proposed in #21
I still don't understand why @ealmansi didn't merge it because it doesn't break anything and it was only one click away. We could have also exported extra method like setIntervalAsync, setIntervalNowAsync but looks like that proposition didn't sound good to him either.
from set-interval-async.
While I understand that there's some interest in this feature, extending vanilla setInterval
other than making it work nicely with async functions is beyond the scope of this library.
from set-interval-async.
Related Issues (19)
- Enough stability for production use HOT 1
- Publish as scoped package HOT 1
- Add a way to change the interval HOT 1
- setIntervalAsync stop working after some hours HOT 3
- 404 error HOT 1
- Importing dependencies like an example in the readme breaks clearIntervalAsync HOT 3
- clear interval from setIntervalAsync inside HOT 1
- first method for es6 import not work in nodejs HOT 1
- Question regarding clearIntervalAsync HOT 1
- Any race condition ? Possibility to pause while inside the setIntervalAsync ? HOT 1
- clearIntervalAsync inside setIntervalAsync callback stops the execution HOT 1
- Error when running with Webpack 5 HOT 1
- Handling computer sleep HOT 1
- Cannot find module 'set-interval-async/dynamic' or its corresponding type declarations. HOT 1
- Cannot be imported to non-TypeScript project after 3.0.0-3.0.2 HOT 2
- /node_modules/set-interval-async/dist/set-interval-async-timer.cjs:25 HOT 4
- Can't import the named export 'clearIntervalAsync' (reexported as 'clearIntervalAsync') from default-exporting module (only default export is available) HOT 1
- Swallowing errors? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from set-interval-async.