ReactiveX / RxPY

ReactiveX for Python

Home Page:https://rxpy.rtfd.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Async map operator

francipvb opened this issue · comments

Is your feature request related to a problem? Please describe.
Sometimes I want to do asynchronous processing of an observable event.

Describe the solution you'd like
Something like reactivex.operators.map, but expecting a function that returns an awaitable based on the event.

import asyncio
from reactivex import  of, pipe
from reactivex.operators import async_map, do_action


def multiply(multiplier: int):
    """An async multiply operator."""

    async def async_multiply(self, x: int) -> int:
        return x*multiplier

    return async_multiply

future = (
    of(3)
    .pipe(async_map(multiply(3)))
    .pipe(do_action(lambda x: print(x)))
)
asyncio.run(future)
# Output: 9

Describe alternatives you've considered
None considered.

Additional context
None considered.

How about converting async function to Observable and using concatMap

Yes, the flap_map mapper may return an asyncio.Future

Yes, the flap_map mapper may return an asyncio.Future

Will check this. The lambda must return an observable to work correctly or a future is enough?

Future or Observable or Iterable

I did this. However, there is a little benefit to add this to the library as an operator.

It's not really possible to get it right since on_next is synchronous to begin with. The alternative is https://github.com/dbrattli/aioreactive

Nice project. I will move to it.