Async/Await
BanglaCode supports asynchronous programming using promises with the proyash (async) and opekha (await) keywords. This allows you to write non-blocking code that runs concurrently.
Key Concepts
proyash(প্রয়াস) - Marks a function as asynchronousopekha(অপেক্ষা) - Waits for a promise to resolve- Promise - A value that will be available in the future
- Concurrent Execution - Multiple async operations run at the same time
Async Functions
Declare an async function using the proyash keyword before kaj:
| 1 | // Async function definition |
| 2 | proyash kaj fetchData() { |
| 3 | dekho("Fetching data..."); |
| 4 | opekha ghumaao(1000); // Sleep for 1 second |
| 5 | dekho("Data fetched!"); |
| 6 | ferao "Data from server"; |
| 7 | } |
| 8 | |
| 9 | // Call async function - returns a promise |
| 10 | dhoro promise = fetchData(); |
| 11 | dekho(promise); // Promise(PENDING) |
Await Keyword
Use opekha to wait for a promise to resolve or reject. Await can only be used inside async functions:
| 1 | proyash kaj main() { |
| 2 | dekho("Starting..."); |
| 3 | |
| 4 | // Wait for async operation to complete |
| 5 | dhoro result = opekha fetchData(); |
| 6 | dekho("Result:", result); // Result: Data from server |
| 7 | |
| 8 | dekho("Finished!"); |
| 9 | } |
| 10 | |
| 11 | // Run the main async function |
| 12 | main(); |
Error Handling with Await
| 1 | proyash kaj riskyOperation() { |
| 2 | opekha ghumaao(500); |
| 3 | felo "Something went wrong!"; |
| 4 | } |
| 5 | |
| 6 | proyash kaj safeRun() { |
| 7 | chesta { |
| 8 | dhoro result = opekha riskyOperation(); |
| 9 | dekho("Success:", result); |
| 10 | } dhoro_bhul (error) { |
| 11 | dekho("Error caught:", error); |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | safeRun(); // Error caught: Something went wrong! |
Built-in Async Functions
ghumaao (ঘুমাও) - Sleep
Pause execution for a specified number of milliseconds:
| 1 | proyash kaj timer() { |
| 2 | dekho("Start"); |
| 3 | opekha ghumaao(1000); // Sleep for 1 second |
| 4 | dekho("1 second later"); |
| 5 | opekha ghumaao(2000); // Sleep for 2 seconds |
| 6 | dekho("3 seconds total"); |
| 7 | } |
| 8 | |
| 9 | timer(); |
sob_proyash (সব_প্রয়াস) - Promise.all
Wait for multiple promises to complete concurrently (all at the same time):
| 1 | proyash kaj fetchUser() { |
| 2 | opekha ghumaao(1000); |
| 3 | ferao "User data"; |
| 4 | } |
| 5 | |
| 6 | proyash kaj fetchPosts() { |
| 7 | opekha ghumaao(1500); |
| 8 | ferao "Posts data"; |
| 9 | } |
| 10 | |
| 11 | proyash kaj fetchComments() { |
| 12 | opekha ghumaao(800); |
| 13 | ferao "Comments data"; |
| 14 | } |
| 15 | |
| 16 | proyash kaj loadDashboard() { |
| 17 | dekho("Loading dashboard..."); |
| 18 | |
| 19 | // Run all three fetches concurrently |
| 20 | dhoro results = opekha sob_proyash([ |
| 21 | fetchUser(), |
| 22 | fetchPosts(), |
| 23 | fetchComments() |
| 24 | ]); |
| 25 | |
| 26 | dekho("All data loaded!"); |
| 27 | dekho("User:", results[0]); |
| 28 | dekho("Posts:", results[1]); |
| 29 | dekho("Comments:", results[2]); |
| 30 | } |
| 31 | |
| 32 | loadDashboard(); |
| 33 | // Takes ~1.5 seconds (longest operation) |
| 34 | // NOT 3.3 seconds (sum of all operations) |
Performance Tip
sob_proyash runs all promises concurrently, not sequentially. This means 3 promises of 500ms each will complete in ~500ms total, not 1500ms!
Async File I/O
| 1 | // poro_async - Read file asynchronously |
| 2 | proyash kaj readConfig() { |
| 3 | dhoro content = opekha poro_async("config.json"); |
| 4 | dekho("Config:", content); |
| 5 | ferao json_poro(content); |
| 6 | } |
| 7 | |
| 8 | // lekho_async - Write file asynchronously |
| 9 | proyash kaj saveData(data) { |
| 10 | dhoro json = json_banao(data); |
| 11 | opekha lekho_async("output.json", json); |
| 12 | dekho("File saved!"); |
| 13 | } |
| 14 | |
| 15 | // Use them |
| 16 | proyash kaj main() { |
| 17 | dhoro config = opekha readConfig(); |
| 18 | opekha saveData(config); |
| 19 | } |
| 20 | |
| 21 | main(); |
Async HTTP Requests
| 1 | // anun_async - Fetch data from URL |
| 2 | proyash kaj fetchAPI() { |
| 3 | dekho("Fetching from API..."); |
| 4 | |
| 5 | dhoro response = opekha anun_async("https://api.example.com/data"); |
| 6 | dekho("Status:", response["status"]); |
| 7 | |
| 8 | dhoro data = json_poro(response["body"]); |
| 9 | dekho("Data:", data); |
| 10 | |
| 11 | ferao data; |
| 12 | } |
| 13 | |
| 14 | fetchAPI(); |
Practical Examples
Sequential vs Concurrent Execution
| 1 | // Sequential (slow) - waits for each operation |
| 2 | proyash kaj sequential() { |
| 3 | dhoro start = somoy(); |
| 4 | |
| 5 | opekha ghumaao(500); // Wait 500ms |
| 6 | opekha ghumaao(500); // Wait 500ms more |
| 7 | opekha ghumaao(500); // Wait 500ms more |
| 8 | |
| 9 | dhoro elapsed = somoy() - start; |
| 10 | dekho("Sequential time:", elapsed, "ms"); // ~1500ms |
| 11 | } |
| 12 | |
| 13 | // Concurrent (fast) - all operations at once |
| 14 | proyash kaj concurrent() { |
| 15 | dhoro start = somoy(); |
| 16 | |
| 17 | dhoro p1 = ghumaao(500); |
| 18 | dhoro p2 = ghumaao(500); |
| 19 | dhoro p3 = ghumaao(500); |
| 20 | |
| 21 | opekha sob_proyash([p1, p2, p3]); |
| 22 | |
| 23 | dhoro elapsed = somoy() - start; |
| 24 | dekho("Concurrent time:", elapsed, "ms"); // ~500ms |
| 25 | } |
| 26 | |
| 27 | sequential(); // Takes ~1500ms |
| 28 | concurrent(); // Takes ~500ms (3x faster!) |
Error Handling with Promise.all
| 1 | proyash kaj task1() { |
| 2 | opekha ghumaao(500); |
| 3 | ferao "Task 1 done"; |
| 4 | } |
| 5 | |
| 6 | proyash kaj task2() { |
| 7 | opekha ghumaao(300); |
| 8 | felo "Task 2 failed!"; |
| 9 | } |
| 10 | |
| 11 | proyash kaj task3() { |
| 12 | opekha ghumaao(700); |
| 13 | ferao "Task 3 done"; |
| 14 | } |
| 15 | |
| 16 | proyash kaj runAll() { |
| 17 | chesta { |
| 18 | // If any promise rejects, sob_proyash rejects immediately |
| 19 | dhoro results = opekha sob_proyash([ |
| 20 | task1(), |
| 21 | task2(), |
| 22 | task3() |
| 23 | ]); |
| 24 | dekho("All tasks succeeded:", results); |
| 25 | } dhoro_bhul (error) { |
| 26 | dekho("One task failed:", error); // Task 2 failed! |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | runAll(); |
Chaining Async Operations
| 1 | proyash kaj getUserId() { |
| 2 | opekha ghumaao(500); |
| 3 | ferao 123; |
| 4 | } |
| 5 | |
| 6 | proyash kaj getUserProfile(userId) { |
| 7 | opekha ghumaao(500); |
| 8 | ferao { naam: "Rahim", id: userId }; |
| 9 | } |
| 10 | |
| 11 | proyash kaj getUserPosts(userId) { |
| 12 | opekha ghumaao(500); |
| 13 | ferao ["Post 1", "Post 2", "Post 3"]; |
| 14 | } |
| 15 | |
| 16 | proyash kaj loadUserData() { |
| 17 | // Step 1: Get user ID |
| 18 | dhoro id = opekha getUserId(); |
| 19 | dekho("Got user ID:", id); |
| 20 | |
| 21 | // Step 2: Load profile and posts concurrently |
| 22 | dhoro results = opekha sob_proyash([ |
| 23 | getUserProfile(id), |
| 24 | getUserPosts(id) |
| 25 | ]); |
| 26 | |
| 27 | dhoro profile = results[0]; |
| 28 | dhoro posts = results[1]; |
| 29 | |
| 30 | dekho("Profile:", profile); |
| 31 | dekho("Posts:", posts); |
| 32 | } |
| 33 | |
| 34 | loadUserData(); |
Timeout Handling
All opekha operations have a built-in 30-second timeout to prevent infinite blocking:
| 1 | proyash kaj neverResolves() { |
| 2 | // This promise never resolves or rejects |
| 3 | // After 30 seconds, await will timeout |
| 4 | } |
| 5 | |
| 6 | proyash kaj testTimeout() { |
| 7 | chesta { |
| 8 | opekha neverResolves(); |
| 9 | } dhoro_bhul (error) { |
| 10 | dekho(error); // await timeout: promise did not resolve within 30 seconds |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | testTimeout(); |
Best Practices
✓ Use concurrent execution when possible
Run independent async operations concurrently with sob_proyash for better performance.
✓ Always handle errors
Wrap async operations in chesta/dhoro_bhul blocks to catch rejections.
✓ Keep async functions focused
Break complex async logic into smaller, composable async functions.
✗ Don't use await in loops unnecessarily
Sequential awaits in loops can be slow. Use sob_proyash to run iterations concurrently.
Summary
| Keyword | Bengali | Purpose |
|---|---|---|
proyash | প্রয়াস | Declare async function |
opekha | অপেক্ষা | Wait for promise |
ghumaao | ঘুমাও | Sleep for milliseconds |
sob_proyash | সব_প্রয়াস | Wait for all promises (concurrent) |
poro_async | পড়ো_async | Read file asynchronously |
lekho_async | লেখো_async | Write file asynchronously |
anun_async | আনুন_async | HTTP GET asynchronously |