Lwt promise local storage, is it possible?

46 views Asked by At

I'm building an application using Lwt, and it would be nice for me to be able to have some sort of context, or promise local static storage for the life cycle of the promise. Is there any way to do this? Ideally it could be a simple Map that is available to each promise.

Ideally it would be like:

val get_lwt_context : 'a Lwt.t -> 'a Map.t

This would return the storage context for promise t. Is this possible? Is there another library that implements this?

1

There are 1 answers

0
Pierre Chambart On

There is indeed such a mechanism in Lwt, called implicit callback argument, see Documentation. It is considered deprecated, because it is too easy to make mistakes with it, but sometimes this is really useful for some things like logging for instance.

Such a mechanism can't be implemented by a library outside of Lwt, this needs to be handled in some way by the scheduler.