New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

react-usetask-z

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-usetask-z

React hook for managing asynchronous tasks with delay, repeat, and auto-restart support

latest
Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
2
-71.43%
Maintainers
1
Weekly downloads
 
Created
Source

react-usetask-z

NPM JavaScript Style Guide Downloads

A lightweight React custom hook for creating flexible tasks/timers.

🔹 Supports sequential or fixed interval tasks 🔹 Repeat tasks a fixed number of times or infinitely 🔹 Cancel, reset, and auto-restart tasks 🔹 Works with async or sync functions

🚀 Live Demo

👉 Codesandbox Example

📦 Installation

npm install react-usetask-z
# or
yarn add react-usetask-z

Import in your project:

import useTask from "react-usetask-z";

🛠 Usage

Initialize a task

const { execute, executeAsync, cancel, reset } = useTask({
  fn: async () => {
    console.log("⚡ Task executed!");
  },
  delay: 1000, // Initial delay in ms
  repeat: 5, // Repeat 5 times, true = infinite
  interval: 500, // Interval between repeats in ms
  mode: "sequential", // sequential | fixed
  retry: 2, // retry 2 times on error
  retryDelay: 1000, // 1s between retries
});

Run task immediately

execute();

Run async task with await

await executeAsync(async () => {
  const response = await fetch("/api/data");
  const data = await response.json();
  console.log("✅ API completed", data);
});

Run after custom delay

execute(() => console.log("⏱ Run after 2 seconds"), 2000);

Stop or reset tasks

cancel(); // 🛑 Stop current task
reset(); // 🔄 Reset repeat count and stop

Sequential vs Fixed mode

  • sequential: waits for previous async task to complete before next iteration
  • fixed: runs tasks on a fixed interval regardless of previous task completion
useTask({ mode: "fixed", interval: 1000 });

Auto-restart

useTask({
  fn: () => console.log("🔄 Restart task"),
  repeat: 3,
  restartDelay: 2000, // restart 2 seconds after completion
});

⚙️ API

FunctionDescription
execute(fn?, delay?, repeat?, interval?, mode?)⚡ Run task (sync / normal callback)
executeAsync(fn?, delay?, repeat?, interval?, mode?)✅ Run task async with promise support
cancel()🛑 Stop current task immediately
reset()🔄 Stop and reset repeat count

✨ Notes

  • 🔁 If repeat is set to true, the task will loop infinitely until cancel() is called
  • ⚡ Use executeAsync if your task returns a promise and you want sequential execution
  • restartDelay allows tasks to automatically restart after finishing all repeats
  • 🔄 Retry mechanism available with retry and retryDelay
  • 🛠 Error handling via onError callback

📋 License

MIT

Keywords

react

FAQs

Package last updated on 26 Sep 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts