libfetch 0.0.0
A lightweight asynchronous HTTP/1.1 client library implementing a subset of the WHATWG Fetch API.
Loading...
Searching...
No Matches
Promise Management

Functions for working with fetch promises. More...

Functions

bool fetch_promise_poll (fetch_promise_t *promise)
 Check if a promise has completed (NON-BLOCKING)
 
bool fetch_promise_await (fetch_promise_t *promise, uint32_t timeout_ms)
 Wait for a promise to complete (BLOCKING)
 
bool fetch_promise_cancel (fetch_promise_t *promise, const char *reason)
 Cancel a pending promise.
 
bool fetch_promise_cancelled (const fetch_promise_t *promise)
 Check if a promise was cancelled.
 

Detailed Description

Functions for working with fetch promises.

Function Documentation

◆ fetch_promise_await()

bool fetch_promise_await ( fetch_promise_t * promise,
uint32_t timeout_ms )

Wait for a promise to complete (BLOCKING)

This function blocks the calling thread and drives the event loop internally until the promise completes or times out.

Parameters
promiseThe promise to wait for
timeout_msMaximum time to wait in milliseconds (0 for infinite)
Returns
true if promise completed within timeout, false if timed out
Warning
This function BLOCKS the calling thread
Note
Event Loop: Drives the event loop internally while waiting
// BLOCKING approach (simpler but blocks thread)
fetch_promise_t *promise = fetch_async("https://httpbin.org/get", NULL);
if (fetch_promise_await(promise, 5000)) { // Blocks for up to 5 seconds
if (fetch_promise_fulfilled(promise)) {
printf("Request completed successfully\n");
} else {
printf("Request failed\n");
}
} else {
printf("Request timed out\n");
}
fetch_promise_t * fetch_async(const char *url, fetch_init_t *init)
Make an asynchronous HTTP request (NON-BLOCKING)
Definition fetch.c:7764
void fetch_promise_free(fetch_promise_t *promise)
Free a promise object.
Definition fetch.c:8401
bool fetch_promise_fulfilled(const fetch_promise_t *promise)
Check if promise was fulfilled successfully.
Definition fetch.c:8020
bool fetch_promise_await(fetch_promise_t *promise, uint32_t timeout_ms)
Wait for a promise to complete (BLOCKING)
Definition fetch.c:7907
Promise for asynchronous fetch operations.
Definition fetch.h:563

◆ fetch_promise_cancel()

bool fetch_promise_cancel ( fetch_promise_t * promise,
const char * reason )

Cancel a pending promise.

Parameters
promiseThe promise to cancel
reasonOptional reason for cancellation
Returns
true if promise was cancelled, false if already completed
fetch_promise_t *promise = fetch_async("https://httpbin.org/delay/10", NULL);
// Cancel after 2 seconds in a separate thread or timer
if (fetch_promise_cancel(promise, "User cancelled")) {
printf("Request was cancelled\n");
}
bool fetch_promise_cancel(fetch_promise_t *promise, const char *reason)
Cancel a pending promise.
Definition fetch.c:7943

◆ fetch_promise_cancelled()

bool fetch_promise_cancelled ( const fetch_promise_t * promise)

Check if a promise was cancelled.

Parameters
promiseThe promise to check
Returns
true if promise was cancelled

◆ fetch_promise_poll()

bool fetch_promise_poll ( fetch_promise_t * promise)

Check if a promise has completed (NON-BLOCKING)

Parameters
promiseThe promise to check
Returns
true if promise is completed (fulfilled or rejected), false if still pending
Note
This function does NOT block and does NOT drive the event loop
You must call fetch_event_loop_process() separately to make progress
// CORRECT: Non-blocking polling
fetch_promise_t *promise = fetch_async("https://httpbin.org/get", NULL);
while (!fetch_promise_poll(promise)) {
// Drive the event loop to make progress
fetch_event_loop_process(10); // Small timeout for responsiveness
// Do other work while waiting
handle_other_tasks();
}
// Promise is now complete
int fetch_event_loop_process(uint32_t timeout_ms)
Process events in the event loop (NON-BLOCKING with timeout)
Definition fetch.c:7339
bool fetch_promise_poll(fetch_promise_t *promise)
Check if a promise has completed (NON-BLOCKING)
Definition fetch.c:7900