Skip to content

Instantly share code, notes, and snippets.

@devonestes
Last active Jun 13, 2018
Embed
What would you like to do?
defmodule SendAfter do
def send_after(pid, message, nanoseconds) do
{_, os_name} = :os.type()
increment =
if os_name == :Windows do
# monotonic time for Windows in miliseconds, in Linux and MacOS it's nanoseconds
# so we'll need to convert to miliseconds for windows
nanoseconds / 1_000_000
else
nanoseconds
end
start_time = :erlang.monotonic_time()
end_time = start_time + increment
Task.async(fn -> wait_and_send(end_time, pid, message) end)
end
def wait_and_send(end_time, pid, message) do
if :erlang.monotonic_time() >= end_time do
send(pid, message)
else
wait_and_send(end_time, pid, message)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment