Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@closeio/use-abortable-effect
Advanced tools
Super simple React hook for running abortable effects
Super simple React hook for running abortable effects based on the AbortController
API.
Interested in working on projects like this? Close is looking for great engineers to join our team!
yarn add @closeio/use-abortable-effect
AbortController
API and it is compatible with the fetch
API.AbortController
API then the hook behaves exactly like a regular useEffect
hook. See Can I Use for browser support overview.useEffect
useEffect
,
where the effect function you pass-in accepts an AbortSignal
instance as a param and you
can return a cleanup function that accepts an AbortController
instance.fetch
requests.useEffect(() => {
// do something
return () => {
/* cleanup */
};
}, [deps]);
const abortControllerRef = useAbortableEffect(
(abortSignal) => {
// do something
return (abortController) => {
/* do cleanup, you should probably abort */
};
},
[deps],
);
fetch
requestsimport React from 'react';
import useAbortableEffect from '@closeio/use-abortable-effect';
export default function MyAbortableFetchComponent() {
const abortControllerRef = useAbortableEffect((abortSignal) =>
fetch(url, { signal: abortSignal })
.then(/* … */)
.catch((rejection) => {
if (rejection.name !== 'AbortError') {
// Re-throw or handle non-abort rejection in another way.
return Promise.reject(rejection);
}
}),
);
const handleManualAbort = () => abortControllerRef.current.abort();
// …
}
import React from 'react';
import useAbortableEffect from '@closeio/use-abortable-effect';
export default function MyAbortableComputationComponent() {
const abortControllerRef = useAbortableEffect(abortSignal => {
new Promise((resolve, reject) => {
// Should be a DOMException per spec.
const abortRejection = new DOMException(
'Calculation aborted by the user',
'AbortError',
);
// Handle when abort was requested before starting the computation.
if (abortSignal.aborted) {
return reject(abortRejection);
}
// This simulates an expensive computation.
const timeout = setTimeout(() => resolve(1), 5000);
// Listen for abort request.
abortSignal.addEventListener('abort', () => {
clearTimeout(timeout);
reject(abortRejection);
});
})
.then(/* … */)
.catch(rejection => {
if (rejection.name !== 'AbortError') {
// Re-throw or handle non-abort rejection in another way.
return Promise.reject(rejection);
}
}),
});
const handleManualAbort = () => abortControllerRef.current.abort();
// …
}
import React from 'react';
import useAbortableEffect from '@closeio/use-abortable-effect';
export default function MyCustomCleanupComponent() {
const [gotAborted, setGotAborted] = useState(false);
const abortControllerRef = useAbortableEffect((abortSignal) => {
fetch(url, { signal: abortSignal })
.then(/* … */)
.catch((rejection) => {
if (rejection.name !== 'AbortError') {
// Re-throw or handle non-abort rejection in another way.
return Promise.reject(rejection);
}
});
// Just return a function like in `useEffect`, with the difference that you
// get the abort controller (not a ref) as a param.
return (controller) => {
controller.abort();
setGotAborted(true);
};
});
const handleManualAbort = () => abortControllerRef.current.abort();
// …
}
MIT © Close
FAQs
Super simple React hook for running abortable effects
The npm package @closeio/use-abortable-effect receives a total of 750 weekly downloads. As such, @closeio/use-abortable-effect popularity was classified as not popular.
We found that @closeio/use-abortable-effect demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.