# `PlaywrightEx.Promise`
[🔗](https://github.com/ftes/playwright_ex/blob/v0.17.0-rc.5/lib/playwright_ex/promise.ex#L1)

A pending result, as returned by the waits that JS starts without `await`
(`PlaywrightEx.Page.wait_for_event/3`, `wait_for_request/3`, `wait_for_navigation/2`, ...):

    popup = PlaywrightEx.Page.wait_for_event(page, :popup)
    PlaywrightEx.Page.click!(page, "a[target=_blank]")
    {:ok, popup} = PlaywrightEx.Promise.await(popup)

The wait is set up before the function returns, so events of the actions that follow are
seen.

A promise is owned by the process that started it. Unlike a plain Elixir `Task`, any process
can await it, any number of times, and gets the same result, which is kept while the owner
lives. Once the owner has exited, awaiting raises `PlaywrightEx.TargetClosedError`. A promise
started inside an event listener is owned by the object it waits on (`page` in
`wait_for_event(page, ...)`) instead, since the listener's process may exit first; it lives
until that object is freed after close.
`free/1` drops a promise you no longer need before its owner exits.

The same waits have a callback form, as in the other official clients
(`PlaywrightEx.Page.expect_popup(page, fn -> ... end)`).

# `t`

```elixir
@type t() :: %PlaywrightEx.Runtime.Promise{
  cancel: term(),
  handlers: term(),
  kind: term(),
  monitor: term(),
  owner: term(),
  pid: term(),
  promises: term(),
  ref: term(),
  result: term(),
  watch: term()
}
```

A promise handle.

# `await`

```elixir
@spec await(t()) :: {:ok, term()} | {:error, PlaywrightEx.error()}
```

Waits for the promise: `{:ok, value}`, or `{:error, exception}` if it is rejected with a
`t:PlaywrightEx.error/0`. Other exceptions (bugs) are raised.

# `await!`

```elixir
@spec await!(t()) :: term()
```

Like `await/1`, but returns the value and raises if the promise is rejected.

# `free`

```elixir
@spec free(t()) :: :ok
```

Elixir only: drops a promise nobody will await, such as a wait started before checking state
that turned out to be ready (see the [Concurrency](concurrency.md) guide). Its result is no longer kept, no
late message reaches the caller; don't await it afterwards. Without `free/1` the result is
kept until the owner exits.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
