# wait_for

The .wait_for() method for the SurrealDB Rust SDK waits for the selected event to happen before proceeding.

Wait for the selected event to happen before proceeding.

```rust title="Method Syntax"
db.wait_for(event)
```

## Arguments
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Argument</th>
            <th colspan="2" scope="col">Type</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Argument">
                <code>event</code>
            </td>
            <td colspan="2" scope="row" data-label="Type">
                <code>WaitFor</code>
            </td>
            <td colspan="2" scope="row" data-label="Description">
                The event to wait for before proceeding.
            </td>
        </tr>
    </tbody>
</table>

`WaitFor` is a simple enum with two variants representing the event to wait for.

```rust
pub enum WaitFor {
    Connection,
    Database,
}
```

## Example usage

The following test from the source code demonstrates the behaviour of the `.wait_for_()` method in a variety of situations.

```rust
use std::task::Poll;

use surrealdb::engine::remote::ws::{Client, Ws};
use surrealdb::opt::auth::Root;
use surrealdb::opt::WaitFor::{Connection, Database};
use surrealdb::Surreal;

use futures::poll;
use std::pin::pin;

#[tokio::main]
async fn main() -> surrealdb::Result<()> {

    // Create an unconnected client
    // At this point wait_for should continue to wait for both the connection and database selection.
    let db: Surreal<Client> = Surreal::init();
    assert_eq!(poll!(pin!(db.wait_for(Connection))), Poll::Pending);
    assert_eq!(poll!(pin!(db.wait_for(Database))), Poll::Pending);

    // Connect to the server
    // The connection event should fire and allow wait_for to return immediately when waiting for a connection.
    // When waiting for a database to be selected, it should continue waiting.
    db.connect::<Ws>("127.0.0.1:8000").await.unwrap();
    assert_eq!(poll!(pin!(db.wait_for(Connection))), Poll::Ready(()));
    assert_eq!(poll!(pin!(db.wait_for(Database))), Poll::Pending);

    // Sign into the server
    // At this point the connection has already been established but the database hasn't been selected yet.
    db.signin(Root {
        username: "root".to_string(),
        password: "secret".to_string(),
    })
    .await
    .unwrap();
    assert_eq!(poll!(pin!(db.wait_for(Connection))), Poll::Ready(()));
    assert_eq!(poll!(pin!(db.wait_for(Database))), Poll::Pending);

    // Selecting a namespace shouldn't fire the database selection event.
    db.use_ns("namespace").await.unwrap();
    assert_eq!(poll!(pin!(db.wait_for(Connection))), Poll::Ready(()));
    assert_eq!(poll!(pin!(db.wait_for(Database))), Poll::Pending);

    // Select the database to use
    // Both the connection and database events have fired, wait_for should return immediately for both.
    db.use_db("database").await.unwrap();
    assert_eq!(poll!(pin!(db.wait_for(Connection))), Poll::Ready(()));
    assert_eq!(poll!(pin!(db.wait_for(Database))), Poll::Ready(()));
    Ok(())
}
```

## See also

* [.wait_for() method on Docs.rs](https://docs.rs/surrealdb/latest/surrealdb/struct.Surreal.html#method.wait_for)
