from typing import Type
from typing import Callable
from typing import Any


async def aexcepts(
    exception_type: Type[Exception], func: Callable, handler: Callable, x: Any
):
    """An async functional try/except block: await and return `func` on `x`.
    If fails with `exception_type`, return the reult of running handler on the error.


    >>> async def araise(x):
    ...     raise ValueError
    ... run_sync(aexcepts(ValueError, araise, lambda e: e, 5))
    ValueError()

    >>> async def a_just(x):
    ...     return x
    ... run_sync(aexcepts(ValueError, a_just, lambda e: e, 5))
    5
    """
    try:
        return await func(x)
    except exception_type as error:
        return handler(error)