Always wrap asynchronous operations (await, suspend functions, try-await) in the appropriate language-specific async context. Top-level await is generally not supported and will cause runtime errors.
Always wrap asynchronous operations (await, suspend functions, try-await) in the appropriate language-specific async context. Top-level await is generally not supported and will cause runtime errors.
JavaScript/Node.js (CommonJS):
// ❌ Incorrect - top-level await
const result = await databases.createDocuments(...);
// ✅ Correct - async IIFE with error handling
(async () => {
try {
const result = await databases.createDocuments(...);
console.log(result);
} catch (error) {
console.error(error);
}
})();
Swift:
// ❌ Incorrect - try-await outside async context
let documentList = try await databases.upsertDocuments(...);
// ✅ Correct - Task with do-catch
Task {
do {
let documentList = try await databases.upsertDocuments(...);
print(documentList);
} catch {
print("Error:", error);
}
}
C#:
// ❌ Incorrect - await outside async method
DocumentList result = await databases.CreateDocuments(...);
// ✅ Correct - async method
public static async Task Main(string[] args)
{
DocumentList result = await databases.CreateDocuments(...);
Console.WriteLine(result);
}
Always include appropriate error handling for asynchronous operations to prevent unhandled promise/task rejections and improve debugging.
Enter the URL of a public GitHub repository