---
title: select
summary: Given a collection of PendingRequests, block until the result of one of the requests is ready.
url: https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/http/request/fn.select/
---

**Kind:** Function  
**Module:** [request](https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/http/request/)  
**Package:** fastly 0.13.1  
**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/request/pending.rs#356

```rust
fn select<I>(pending_reqs: I) -> (Result<Response, SendError>, Vec<PendingRequest>)
where
    I: IntoIterator<Item = PendingRequest>,
```

Given a collection of [`PendingRequest`](https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/http/request/struct.PendingRequest/)s, block until the result of one of the requests is
ready.

This function accepts any type which can become an iterator that yields requests; a common
choice is `Vec<PendingRequest>`.

Returns a tuple `(result, remaining)`, where:

- `result` is the result of the request that became ready.

- `remaining` is a vector containing all of the requests that did not become ready. The order of
the requests in this vector is not guaranteed to match the order of the requests in the argument
collection.

###### Examples

**Selecting using the request URI**

You can use [`Response::get_backend_request()`](https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/struct.Response/#method-get_backend_request) to inspect the request that a response came
from. This example uses the URL to see which of the two requests finished first:

```rust
use fastly::{Error, Request};

// Send two asynchronous requests, and store the pending requests in a vector.
let req1 = Request::get("http://www.origin.org/meow")
    .send_async("TheOrigin")?;
let req2 = Request::get("http://www.origin.org/woof")
    .send_async("TheOrigin")?;
let pending_reqs = vec![req1, req2];

// Wait for one of the requests to finish.
let (resp, _remaining) = fastly::http::request::select(pending_reqs);

// Return an error if the request was not successful.
let resp = resp?;

// Inspect the response metadata to see which backend this response came from.
match resp
    .get_backend_request()
    .unwrap()
    .get_url()
    .path()
{
    "/meow" => println!("I love cats!"),
    "/woof" => println!("I love dogs!"),
    _ => panic!("unexpected result"),
}

```

**Selecting using the backend name**

You can also use [`Response::get_backend_name()`](https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/struct.Response/#method-get_backend_name) to identify which pending request in the given
collection finished. Consider this example, where two requests are sent asynchronously to two
different backends:

```rust
use fastly::{Error, Request};

// Send two asynchronous requests, and store the pending requests in a vector.
let req1 = Request::get("http://www.origin-1.org/")
    .send_async("origin1")?;
let req2 = Request::get("http://www.origin-2.org/")
    .send_async("origin2")?;
let pending_reqs = vec![req1, req2];

// Wait for one of the requests to finish.
let (resp, _remaining) = fastly::http::request::select(pending_reqs);

// Return an error if the request was not successful.
let resp = resp?;

// Inspect the response to see which backend this response came from.
match resp.get_backend_name().unwrap() {
    "origin1" => println!("origin 1 responded first!"),
    "origin2" => println!("origin 2 responded first!"),
    _ => panic!("unexpected result"),
}

```

###### Panics

Panics if the argument collection is empty, or contains more than
[`fastly_shared::MAX_PENDING_REQS`](https://docs.rs/fastly_shared/latest/fastly_shared/constant.MAX_PENDING_REQS.html) requests.

**Parameters**

- `pending_reqs`: `I`

**Returns**

`(Result<crate::Response, super::SendError>, Vec<PendingRequest>)`
