Sevoral Error while using AbortController
I want to allow users to abort the request. So the flow goes like this, they upload image i set imageLoading signal to true and display cancel button if they click cancel button the same function will be called and it will check if imageLoading is true if it is true I want to abort the request here is some of my code
const handleFilePreview = async (file) => {
setImageLoading(true)
abortController = new AbortController();
if (imageLoading()) {
abortController.abort()
} else {
setImageLoading(true);
}
try {
const response = await preview_image(file, props.user().profId, {
signal: abortController.signal
})
if (response) {
batch(() => {
setFile(file)
setImageLoading(false)
setImageUrl(response)
})
}
} catch (error) {
console.log(error.name, error.message)
if (error.name === "AbortError") {
return setImageLoading(false)
}
}
}
6 Replies
It is throwing sevoral error in this case it might be because of way I send signal but I think that should be syntax in this case
I modifed the code a bit it was aborting instantly not even sending to server function here is updated code I have same sevoral error
const handleFilePreview = async (file) => {
setImageLoading(true)
const abortController = new AbortController();
console.log(file)
if (imageLoading() && !file) {
abortController.abort()
}
console.log(abortController.signal)
try {
const response = await preview_image(file, props.user().profId, {
signal: abortController.signal
})
if (response) {
batch(() => {
setFile(file)
setImageLoading(false)
setImageUrl(response)
})
}
} catch (error) {
console.log(error.name, error.message)
if (error.name === "AbortError") {
return setImageLoading(false)
}
}
}
if
preview_image
is a server function, AbortSignal
isn't a serializable value. See https://github.com/lxsmnsyc/seroval/blob/main/docs/compatibility.md#supported-typesGitHub
seroval/docs/compatibility.md at main · lxsmnsyc/seroval
Stringify JS values. Contribute to lxsmnsyc/seroval development by creating an account on GitHub.
So what should I do
I did it using API
I'm lacking context here to see the whole picture or the error. It would only get to seroval if it was being sent back to be serialized and if this is just some client logic against an API endpoint I don't know why seroval would be involved.
if I understood correctly he's passing an object that contains an AbortSignal (a value unsupported by seroval) to a server function, expecting control on request cancellation
I'm not seeing anything returned in the example is why I ask. This looks like client code awaiting some preview_image function. That one could be a server function but the abort controller is on the outside. If this is server code hitting a different server then I'd expect something being returned..