Back
Agents
3 min read

Async Programming with the .do JavaScript SDK

The modern web and server environments thrive on asynchronous operations. Waiting for network requests, database queries, or external service calls in a synchronous, blocking manner can cripple performance and responsiveness. This is where asynchronous programming shines, allowing your application to continue executing tasks while waiting for a long-running operation to complete.

The .do SDK is designed with asynchronous operations at its core, particularly when interacting with the .do platform and managing agentic applications. This enables developers to build highly efficient and scalable applications that leverage the power of the .do platform without getting bogged down by waiting states.

Why Async Matters with the .do SDK

When you interact with the .do platform using the SDK, you'll frequently be making calls to its APIs, deploying agents, or triggering workflows. These operations are inherently asynchronous. They involve communication over a network, processing on remote servers, and potentially queuing tasks.

Using asynchronous programming patterns with the .do SDK ensures that:

  • Responsiveness: Your application remains responsive to user input or other events while platform operations are in progress.
  • Efficiency: You can initiate multiple operations concurrently, maximizing resource utilization.
  • Scalability: As your application grows and interacts with the .do platform more frequently, asynchronous operations prevent bottlenecks.

Asynchronous Patterns in the .do JavaScript SDK

The .do JavaScript SDK leverages modern JavaScript features to facilitate asynchronous programming, primarily focusing on Promises and async/await.

Promises: Promises represent the eventual result of an asynchronous operation. They can be in one of three states: pending, fulfilled (successful), or rejected (failed). Promises provide a cleaner way to handle asynchronous results and chain operations compared to traditional callback hell.

import { Agent } from '@do/sdk';

const myAgent = new Agent('my-agent-id');

myAgent.run({
  input: { data: 'process this' }
})
.then(result => {
  console.log('Agent execution complete:', result);
})
.catch(error => {
  console.error('Agent execution failed:', error);
});

async/await: Built on top of Promises, async/await provides a syntactic sugar that makes asynchronous code look and behave much more like synchronous code, enhancing readability and maintainability.

import { Agent } from '@do/sdk';

async function runMyAgent() {
  const myAgent = new Agent('my-agent-id');

  try {
    await myAgent.run({
      input: { data: 'process this' }
    });
    console.log('Agent execution complete');
  } catch (error) {
    console.error('Agent execution failed:', error);
  }
}

runMyAgent();

As you can see in the example code snippet from the website hero section:

import { Agent } from '@do/sdk';

const myAgent = new Agent('my-agent-id');

await myAgent.run({
  input: { data: 'process this' }
});

console.log('Agent execution complete');

The await keyword pauses the execution of the async function until the myAgent.run() Promise resolves, providing a clear and sequential flow while still operating asynchronously in the background.

Best Practices for Async Programming with the SDK

  • Embrace async/await: For new code and refactoring, prefer async/await for its improved readability and error handling capabilities.
  • Handle Errors: Always include try...catch blocks with async/await or .catch() with Promises to gracefully handle potential errors during asynchronous operations.
  • Understand Concurrency: Use Promise.all or similar constructs when you need to perform multiple asynchronous operations concurrently and wait for all of them to complete.
  • Avoid Blocking Code: Ensure your asynchronous operations don't block the event loop, especially in server-side JavaScript environments.

Getting Started

As highlighted in the FAQs, starting with the .do SDK is straightforward. You can download the SDK and follow the documentation to integrate it into your projects. The provided quickstart guides and tutorials will help you get up and running with asynchronous interactions with the .do platform quickly.

Develop. Integrate. Extend. with the .do SDK and build powerful, responsive agentic applications using the power of asynchronous programming.

Ready to start building? Explore the .do SDK documentation and unlock the full potential of the .do platform.

Do Work. With AI.

Async Programming with the .do JavaScript SDK