When implementing async operations with cancellation support, ensure that cancelled operations do not continue to update application state and that all cleanup logic is properly consolidated. This prevents race conditions where cancelled requests might still modify data after cancellation.
When implementing async operations with cancellation support, ensure that cancelled operations do not continue to update application state and that all cleanup logic is properly consolidated. This prevents race conditions where cancelled requests might still modify data after cancellation.
Key practices:
Example implementation:
clear: () => {
if (options.abortController) {
options.abortController.abort(new DOMException('Request aborted as the async data was cleared.', 'AbortError'))
}
if (nuxtApp._asyncDataPromises[key.value]) {
(nuxtApp._asyncDataPromises[key.value] as any).cancelled = true
}
}
// Listen for external cancellation
options.abortController.signal.addEventListener('abort', () => {
if (nuxtApp._asyncDataPromises[key.value]) {
(nuxtApp._asyncDataPromises[key.value] as any).cancelled = true
}
})
// Consolidate cleanup in existing disposal handler
if (hasScope) {
onScopeDispose(() => {
off()
stopPolling() // Add to existing handler rather than creating new one
})
}
Enter the URL of a public GitHub repository