# Executing Queries

Use a [DataSource](/wirebootstrap/reference/wire.data/wire.data.datasource.md) object to execute queries in a data service and retrieve the results.

## ExecAsync

The data source `execAsync` method returns a [deferred jQuery promise](https://api.jquery.com/deferred.promise).  Use this asynchronous method to execute the query in the data service.

```javascript
const accountService = new wire.data.DataSource("custom", {
    ServiceRoot: "./account"
});

const query = wire.data.select()
    .from("Users")
    .where().eq("Active", true);
    
const users = await accountService.execAsync(query);
```

### Short-Hand

Queries can also be executed directly from both a [TableQuery](/wirebootstrap/creating-queries/select-queries.md) and [StoredProcedure](/wirebootstrap/creating-queries/stored-procedures.md) object using the `execAsync` method which takes the data source as a parameter.

```javascript
...
const users = await wire.data.select()
    .from("Users")
    .where().eq("Active", true)
    .execAsync(accountService);
```

### TypeScript

TypeScript requires a method to return a specific type.  To cast the response from the `execAsync` method to a type use the `as undefined` pattern.

In the example below,  the results can be cast directly into an array of `User` class objects by first casting them as `undefined`.

```typescript
const accountService = new wire.data.DataSource("custom", {
    ServiceRoot: "./account"
});

const query = wire.data.select()
    .from("Users")
    .where().eq("Active", true);
    
const users = await accountService.execAsync(query) as undefined as Array<User>;
```

### DataTables

The format of asynchronous results depends on how the data connector returns the data.  In the example above, the [Custom Data Connector](https://docs.wirebootstrap.com/wirebootstrap/connecting-to-data/data-connectors/custom-web-services) results are returned as an array of plain JavaScript objects containing the user data.

An instance of [WireBootstrap's DataTable](/wirebootstrap/working-with-datatables/datatables.md) can also be created from the array in order to work with the data locally.

```typescript
const users = await accountService.execAsync(query) as undefined as Array<User>

const userTable = new wire.data.DataTable(users);
```

In the example below, the [Query Service](https://docs.wirebootstrap.com/query-service) service provider returns a data table automatically.

```typescript
const accountService = new wire.data.DataSource("sql", {
    ServiceRoot: "./account"
});

const query = wire.data.select()
    .from("Users")
    .where().eq("Active", true);
    
const userTable = await accountService.execAsync(query) as undefined as IWireDataTable
```

Sometimes the format of the data returned from the data service call is unknown.  The `getResponseTable` method on the data source can be used to transform the response data into a data table.  Here, the data source will reach into the service provider for this transform as it knows the native format of the result set.

```typescript
...
    
const userData = await accountService.execAsync(query);

const userTable: IWireDataTable = accountService.getResponseTable(userData);
```

## Exec

The `exec` method is the synchronous equivalent of `execAsync`.  This method will pause the thread and wait for the results to come back from the data service call.

```javascript
...
const users = accountService.exec(query);
```

For details on these data source methods, visit the [DataSource](/wirebootstrap/reference/wire.data/wire.data.datasource.md) reference page.  For details on data tables, visit the [DataTable](/wirebootstrap/reference/wire.data/wire.data.datatable.md) reference page.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.wirebootstrap.com/wirebootstrap/creating-queries/query-results.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
