Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
awesome-debounce-promise
Advanced tools
Debounce your async calls with React in mind.
Forget about:
From the author of this famous SO question about debouncing with React.
yarn add awesome-debounce-promise
npm install awesome-debounce-promise --save
import AwesomeDebouncePromise from 'awesome-debounce-promise';
const asyncFunction = () => fetch('/api');
const asyncFunctionDebounced = AwesomeDebouncePromise(
asyncFunction,
500,
options,
);
const searchAPI = text => fetch('/search?text=' + encodeURIComponent(text));
const searchAPIDebounced = AwesomeDebouncePromise(searchAPI, 500);
class SearchInputAndResults extends React.Component {
state = {
text: '',
results: null,
};
handleTextChange = async text => {
this.setState({ text, results: null });
const result = await searchAPIDebounced(text);
this.setState({ result });
};
componentWillUnmount() {
this.setState = () => {};
}
}
When calling debouncedSearchAPI
:
this.setState({ result });
call per api callconst saveFieldValue = (fieldId, fieldValue) =>
fetch('/saveField', {
method: 'PUT',
body: JSON.stringify({ fieldId, fieldValue }),
});
const saveFieldValueDebounced = AwesomeDebouncePromise(
saveFieldValue,
500,
// Use a key to create distinct debouncing functions per field
{ key: (fieldId, text) => fieldId },
);
class SearchInputAndResults extends React.Component {
state = {
value1: '',
value2: '',
};
onFieldTextChange = async (fieldId, fieldValue) => {
this.setState({ [fieldId]: fieldValue });
await saveFieldValueDebounced(fieldId, fieldValue);
};
render() {
const { value1, value2 } = this.state;
return (
<form>
<input
value={value1}
onChange={e => onFieldTextChange(1, e.target.value)}
/>
<input
value={value2}
onChange={e => onFieldTextChange(2, e.target.value)}
/>
</form>
);
}
}
Thanks to the key
feature, the 2 fields will be debounced independently from each others. In practice, one debounced function is created for each key.
const DefaultOptions = {
// One distinct debounced function is created per key and added to an internal cache
// By default, the key is null, which means that all the calls
// will share the same debounced function
key: (...args) => null,
// By default, a debounced function will only resolve
// the last promise it returned
// Former calls will stay unresolved, so that you don't have
// to handle concurrency issues in your code
// Setting this to false means all returned promises will resolve to the last result
onlyResolvesLast: true,
};
Other debouncing options are available and provided by an external low-level library: debounce-promise
You can easily add promise cancellation support to this lib with awesome-imperative-promise, lib that is already used internally.
The debouncing function returned by the lib is stateful. If you want deboucing to work fine, make sure to avoid recreating this function everytime. This is the same behavior as regular callback-based debouncing functions.
Instead of this:
handleTextChange = async text => {
const searchAPI = text => fetch('/search?text=' + encodeURIComponent(text));
const searchAPIDebounced = AwesomeDebouncePromise(searchAPI, 500);
this.setState({ text, results: null });
const result = await searchAPIDebounced(text);
this.setState({ result });
};
Do this:
const searchAPI = text => fetch('/search?text=' + encodeURIComponent(text));
const searchAPIDebounced = AwesomeDebouncePromise(searchAPI, 500);
handleTextChange = async text => {
this.setState({ text, results: null });
const result = await searchAPIDebounced(text);
this.setState({ result });
};
Looking for a React/ReactNative freelance expert with more than 5 years production experience? Contact me from my website or with Twitter.
FAQs
Debounce your async calls
The npm package awesome-debounce-promise receives a total of 60,189 weekly downloads. As such, awesome-debounce-promise popularity was classified as popular.
We found that awesome-debounce-promise demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.