-
Notifications
You must be signed in to change notification settings - Fork 79
Closed
Description
The Problem
Service calls currently only support callbacks, which doesn't fit well with modern JavaScript patterns. You can't use async/await, making error handling awkward and code harder to read.
Current (callback-based):
client.sendRequest(request, (response) => {
// Can't easily handle errors
// Can't use try/catch
// Hard to chain multiple calls
// No timeout support
});What we want (async/await):
try {
const response = await client.sendRequest(request);
// Clean, readable code
// Proper error handling
// Easy to chain calls
} catch (error) {
// Handle errors naturally
}Current Workaround
We have to manually wrap every service call in a Promise:
function callServiceAsync(client, request) {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject(new Error('Timeout')), 5000);
client.sendRequest(request, (response) => {
clearTimeout(timeout);
resolve(response);
});
});
}
// Then use it everywhere
const response = await callServiceAsync(client, request);This adds boilerplate to every service call in every project.
Proposed Solution
Add async/await support to service calls with optional timeout and cancellation:
// Simple promise-based call
const response = await client.sendRequestAsync(request);
// With timeout
const response = await client.sendRequestAsync(request, { timeout: 5000 });
// With AbortController (modern cancellation pattern)
const controller = new AbortController();
const response = await client.sendRequestAsync(request, {
signal: controller.signal
});
// Cancel if needed
controller.abort();Real Usage
Before:
client.sendRequest(request1, (response1) => {
client.sendRequest(request2, (response2) => {
client.sendRequest(request3, (response3) => {
// Callback hell
});
});
});After:
try {
const response1 = await client.sendRequestAsync(request1);
const response2 = await client.sendRequestAsync(request2);
const response3 = await client.sendRequestAsync(request3);
// Clean sequential calls
} catch (error) {
console.error('Service call failed:', error.message);
}Implementation Notes
- Keep existing
sendRequest(request, callback)for backward compatibility - Add new
sendRequestAsync(request, options)method - Support timeout via options or AbortSignal
- Works with all existing service types
Metadata
Metadata
Assignees
Labels
No labels