jarvism

jarvism

Share variable with all functions in a module

Hello, everyone!
I am having a really hard time wrapping my head around this one for some reason.
I want to get a token, store it in memory in a module, have it available for all the functions in the module, and update it when the token expires.
In other languages, I might do something like:

token = nil

def handle_token() do
  # Check if I already have a token
  if token do
    # If so, use it.
    token
  else
    # If not (or if it's expired), get a new one.
    token = Req.get(www.token.com)
  end
end

This is obviously oversimplified, but I think you can understand what I mean. What is the idiomatically correct way to do this in Elixir?
I have looked into using ETS for this, but it seems like a lot.

I look forward to your feedback, and thank you!

Most Liked

dimitarvp

dimitarvp

Simply put, you don’t do that in an FP language (though there are escape hatches like ETS indeed).

You’ll get better responses if you specify your original problem – and not how you want to solve it.

gpopides

gpopides

Are you looking how you can handle state? If yes you can use Agents for storing something simple (a string)

As mentioned, you cant do what you are trying because modules are not classes, they don’t contain state.

For that you need a GenServer (which agent is just more simplified)

masudme09

masudme09

For a similar case, when I need to use and update something, I use the following kinda programming pattern. But as @hlx mentioned, Genserver might serve best for your case without further knowing the context of your original problem.

defmodule SomeModule do
@state %{token: nil}

def some_function(params, state) do
 state
end

def another_function(params, params2, state) do
state
end

def update_token(state) do
# update the token
%{token: updated_token}
end

end

So, I try to pass around states, between functions, so any update that happens in any function should be available to the next one. Not sure it serves your purpose, maybe you can add further context to it.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement