Abort Fetch API Requests using AbortController

  Рет қаралды 12,877

Colby Fayock

Colby Fayock

Күн бұрын

Пікірлер: 62
@colbyfayock
@colbyfayock 5 ай бұрын
Learn how to build a full stack Next.js app in my upcoming course: colbyfayock.com/course
@rahulpagidimarri4677
@rahulpagidimarri4677 4 ай бұрын
Hi Thankyou for the video in this case for each render in useEffect you have created new AbortController instance ,Simple before making a fetch call can't we just call abort() method there ? Once we call that method it cancels the previous api request right ? Please correct me if I'm missing something Thanks
@colbyfayock
@colbyfayock 4 ай бұрын
@@rahulpagidimarri4677 hey not totally following the question about what you're trying to do. whats the use case of what you're trying to achieve?
@sohaildarwajkar9979
@sohaildarwajkar9979 7 күн бұрын
First Video related to education that i ever watched in 1x speed..Your way of speaking,teaching and demonstrating is way too captivating.Bless u bro
@colbyfayock
@colbyfayock 7 күн бұрын
🙏
@JasonLayton
@JasonLayton 11 ай бұрын
Finally! Someone demonstrated how to do it outside of a useEffect. This was so difficult to find, great tutorial. Thank you!
@colbyfayock
@colbyfayock 11 ай бұрын
no problem!!
@maddytyagi8988
@maddytyagi8988 3 ай бұрын
being serious you are amazing, it’s after 2 days of constant search . i got my answer
@RefuelTheRocket
@RefuelTheRocket Жыл бұрын
Thank you colleague, you really helped me, i spend a day trying to understand, how it works, and with ur video i finally done it!
@colbyfayock
@colbyfayock Жыл бұрын
You're welcome. Glad it helped
@JonatanKruszewski
@JonatanKruszewski 11 ай бұрын
Love your style in how you explain. Neat, clear. Subscribed.
@colbyfayock
@colbyfayock 11 ай бұрын
thank you!!
@jawyor-k3t
@jawyor-k3t Жыл бұрын
Great demonstration Colby, as always. The example project is on spot in terms of illustrating the issue and the solution. I have one question. As I understood, the AbortController cancels requests that are initiated but have not been completed yet. So, let's assume fetch returns responses pretty quickly in my connection. Does this mean that I have to type letters quickly for abortcontroller to cancel? And if I type slowly all the previous letters are going to get through. I know you mentioned throttling and stuff at the end, that's probably the solution. I was just wondering about AbortController
@colbyfayock
@colbyfayock Жыл бұрын
yeah - that's absolutely right. if there's no active request, we can't really cancel it. that's why i had to add the setTimeout inside of the API endpoint because it was too fast, though you could use the Network Tab throttling to potentially see this effect too when testing
@bandekhoda7801
@bandekhoda7801 11 ай бұрын
Very useful! Previously I didn't know how to implement an AbortController in a handler function because of the reasons you listed, so I always just defaulted to using useEffect with the cleanup function being controller.abort LOL
@colbyfayock
@colbyfayock 11 ай бұрын
haha glad it helped!
@woodylucas
@woodylucas 5 күн бұрын
Good stuff Colby!
@colbyfayock
@colbyfayock 5 күн бұрын
thank you!
@jsde_coder3234
@jsde_coder3234 5 ай бұрын
really helped me out today. Thank you for this video!
@colbyfayock
@colbyfayock 5 ай бұрын
awesome no problem!
@hardikcc
@hardikcc 5 ай бұрын
Thanks Colby, this was a great help.
@colbyfayock
@colbyfayock 5 ай бұрын
no problem!
@Elator11777
@Elator11777 7 ай бұрын
This looks nice, however, in a real production envirionment, you'd never have a search function inside of your component, what if it is imported from utils? How do you handle the abort requests in it? Can you pass refs to an imported function?
@antoninogargiulo2308
@antoninogargiulo2308 7 ай бұрын
Great video! What if I have an error boundary (Next.js), and I don't want it to be triggered by cancelled request? Should I check the type of the error catched in the catch block?
@colbyfayock
@colbyfayock 7 ай бұрын
did the cancelled request trigger the error boundary? i dont remember having that issue in my code wrapped in a try/catch: github.com/colbyfayock/my-abort-requests/blob/main/src/components/Search/Search.tsx perhaps if you show me the code i can get a better idea to try to reproduce and see
@tchatchabr
@tchatchabr 2 ай бұрын
Is it a good approach to create a custom hook for abortController and then use it in any component that we need it?
@colbyfayock
@colbyfayock 2 ай бұрын
it probably would make sense for the whole request to be a custom hook as opposed to just the controller
@SyedNaqviwork
@SyedNaqviwork 9 ай бұрын
very well explained you just earned a sub
@colbyfayock
@colbyfayock 9 ай бұрын
thank you!
@irfansaeedkhan7242
@irfansaeedkhan7242 Жыл бұрын
wow , thanks man, i was searching for something same but when user clicks multiple links to different pages, i want it to abort all others and just focus on the last one, also if person is fetching multiple apis data it should about all and only fetch the last one in react/nextjs app, i am unable to do that or simply logic how to do it in whole application at globa,l level
@colbyfayock
@colbyfayock Жыл бұрын
glad it helped! for doing something globally you would need to be making all requests through some kind of single queue that's accessible globally
@Shawn-Mosher
@Shawn-Mosher Жыл бұрын
Couldn’t you use debounce so it’s only fired once when the user stops typing?
@colbyfayock
@colbyfayock Жыл бұрын
totally! i mention that in there. or maybe i mentioned throttle as opposed to denounce, but generally, yeah. being able to combine the two would likely be the best bet between having a responsive enough UI and still managing the async requests getting fired out
@devT44
@devT44 10 ай бұрын
The error we're getting here is same as CancelledError in axios?
@colbyfayock
@colbyfayock 10 ай бұрын
unfamiliar with axios's handling
@terryellis333
@terryellis333 Жыл бұрын
Does this work for cancelling put reqeusts also, or just fetch? I'm having difficulty getitng it to cancel a PUT
@colbyfayock
@colbyfayock Жыл бұрын
seemed to work for me! d.pr/i/hDNmgf i tested by simply updating my test API route to PUT: github.com/colbyfayock/my-abort-requests/blob/main/src/app/api/search/route.ts#L5 and the method on the fetch: github.com/colbyfayock/my-abort-requests/blob/main/src/components/Search/Search.tsx#L35
@terryellis333
@terryellis333 Жыл бұрын
@@colbyfayock Ok thanks; yea trying to figure out why this controller.abort isn't actually cancelling anything for me.
@colbyfayock
@colbyfayock Жыл бұрын
@@terryellis333is it possible the request is completing before it has a chance to cancel? on my local machine i had to add a timeout for testing purposes
@terryellis333
@terryellis333 Жыл бұрын
@@colbyfayock Well what's happening is, our web app is doing puts after every field of a form (no submit button :( ) and they're stacking up... and of course we have a race condition. I followed your example and set up an abort controller using a ref; then we're doing an await on a call that does an axios put to store the data and I'm passing in the abort controller to set the signal. I was expecting the first call to get cancelled, instead both complete with a 200 :(
@colbyfayock
@colbyfayock Жыл бұрын
@@terryellis333got it okay, im not familiar with how this works with Axios, potentially an issue there? but you might have some luck additionally adding throttling on the requests, to prevent the requests from occurring in the first place. that way you're avoiding sending too many requests, but if requests do trigger too quickly still due to differing factors, you have the abortcontroller to manage the cancellation
@rg-s8x
@rg-s8x 3 ай бұрын
Great explanation, however, on a lighter note, for simple use case like the input key debouncing is a better option.
@colbyfayock
@colbyfayock 3 ай бұрын
best to do both :)
@rg-s8x
@rg-s8x 3 ай бұрын
@@colbyfayock agree!
@rigbyb
@rigbyb Жыл бұрын
Thanks, helped me understand it :)
@colbyfayock
@colbyfayock Жыл бұрын
no problem!
@sankaranarayanankm7049
@sankaranarayanankm7049 8 ай бұрын
which is better cleanup of useEffect or abort controller??
@colbyfayock
@colbyfayock 8 ай бұрын
hey im not totally following your question - but i believe aborting in a useEffect cleanup function would be a good pattern to prevent the request from continuing
@victora.duenasrobles7325
@victora.duenasrobles7325 19 күн бұрын
You saved my day :D
@colbyfayock
@colbyfayock 17 күн бұрын
🫡
@tienhuynh2435
@tienhuynh2435 2 ай бұрын
well done sir
@colbyfayock
@colbyfayock 2 ай бұрын
thank you 🙏
@serkanakman9945
@serkanakman9945 5 ай бұрын
Thanks a lot!
@colbyfayock
@colbyfayock 5 ай бұрын
np!
@akylbekrysbekov
@akylbekrysbekov 11 ай бұрын
but i think we can solve this problem with debounce
@colbyfayock
@colbyfayock 11 ай бұрын
I think I mention this in the video but the right solution would be to have both for the use case
@JavascriptForEverything
@JavascriptForEverything 11 ай бұрын
nice one
@colbyfayock
@colbyfayock 11 ай бұрын
Thanks!
@ken2ker495
@ken2ker495 8 ай бұрын
can you give me the api of pixar movie , please ? :>
@colbyfayock
@colbyfayock 8 ай бұрын
hey unfortunately it's not a public API, its just an endpoint that i made that searches a static JSON file. you can find that code here and spin it up though! github.com/colbyfayock/my-abort-requests/blob/main/src/app/api/search/route.ts
The Power of AbortController in JavaScript
17:56
Software Developer Diaries
Рет қаралды 10 М.
Warn Users When Leaving a Page in React with beforeunload
10:22
Colby Fayock
Рет қаралды 18 М.
REAL or FAKE? #beatbox #tiktok
01:03
BeatboxJCOP
Рет қаралды 18 МЛН
小丑教训坏蛋 #小丑 #天使 #shorts
00:49
好人小丑
Рет қаралды 54 МЛН
Sigma Kid Mistake #funny #sigma
00:17
CRAZY GREAPA
Рет қаралды 30 МЛН
How to Abort a Fetch Request
7:40
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 14 М.
Why I avoid useEffect For API Calls and use React Query instead
17:45
This Fixes CORS
13:02
Colby Fayock
Рет қаралды 7 М.
Download Progress Bar in React with Fetch API
19:38
Colby Fayock
Рет қаралды 13 М.
Будем использовать AbortController чаще!
5:55
АйТи Синяк
Рет қаралды 10 М.
Goodbye, useEffect - David Khourshid
29:59
BeJS
Рет қаралды 505 М.
Прерываем асинхронные операции с помощью AbortController
30:11
Why you should clean up react effects using fetch
7:27
Web Dev Cody
Рет қаралды 10 М.
Fetching Data in React - Complete Tutorial
29:10
Cosden Solutions
Рет қаралды 153 М.
REAL or FAKE? #beatbox #tiktok
01:03
BeatboxJCOP
Рет қаралды 18 МЛН