-
-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Making requests with timeout #10
Comments
Good idea. What might the API be? |
Possibly That would basically just be replicating the javascript way of doing it though. Not sure if we want to expose friendlier APIs, or just stick with the most javascripty way. |
For any package I maintain all APIs must be designed with Gleam in mind, in order to get the best experience for the language. |
Okay, well, in that case I guess the first question is what functionality we want to expose. At its simplest, javascript's signal approach gives you a function that you can call to abort the request. You can also combine signals using Not sure what you think about this API suggestion for manipulating options? #5 Based on that, my first thought was something like: let #(abort, options) = fetch.with_abort_signal(options)
fetch.with_timeout(options, 5000) But I guess that would be awkward when it comes to chaining options functions, seeing as one wants to give you the abort function as well as the new options. It's also a little tricky as it's not just an array of signals we can append to, they all need to be combined in one go using But I guess we could keep the signals in an array and then bundle them up when sending the request.. or.. ...alternatively we do something like: let #(abort, signal1) = fetch.abort_signal()
let signal2 = fetch.timeout_signal(5000)
fetch.with_signals(options, [signal1, signal2])
// and maybe a shortcut
fetch.with_timeout(options, 5000) Anyway, that's what my gleam-novice brain managed to squeeze out 🍋 https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal |
What are some real-world use cases here? If we gather those we can see what functionality folks are largely using today in JS. |
@lpil real world use cases are a great place to start! Timeouts:
Abort signals:
Combined abort signals:
|
Ah thank you, I'm understanding a lot better now. I think it would be good to introduce some configuration API and an abortcontroller type which can be optionally taken by this type. This will be more verbose in some contexts but it's the most flexible. We can add more concise functions later if need be. |
Specifying a timeout when making a web request seems like a core feature, otherwise your code is at the whim of whatever you're communicating with.
Usually I'd pass a
AbortSignal.timeout(time)
in the fetch options.Another solution would be to race the promise with setTimeout. Doesn't look like the promises package exposed race functionality though.
Possibly linked with #4.. depending on the approach
Related:
gleam-lang/javascript#11
gleam-lang/javascript#10
The text was updated successfully, but these errors were encountered: