You can now pass a callback function to fetch
and fetch
will pass the response to that callback function when it's received instead of blocking the rest of the code until it receives the response. Here's how you can do it:
// Fetch, in async mode, returns a FetchClient instead of returning the response.
FetchClient client;
void handleResponse(Response response) {
Serial.println(response);
}
void setup() {
// Setting options.
RequestOptions options;
options.method = "GET";
// Making the request.
client = fetch("http://api.grandeur.tech", options, handleResponse);
Serial.println("This now prints before the response is printed.");
}
void loop() {
// This is crucial, it listens on the connection for the response, until it's received.
client.loop()
}
If you want to do more than one fetch
, you need to create more than one FetchClient
variables and more than one client.loop()
s.
Only if you are doing fetch
sequentially, that is, the next fetch
happens after the previous fetch
response has returned, then you can reuse the same FetchClient
.