Running async Lua function in Neovim

Solution for Running async Lua function in Neovim
is Given Below:

I’m trying to make Vim change its theme automatically in daylight, by running a check every 30 seconds. I am following this guide. However, I want to port his Vimscript function to Lua with the help of this website. The problem is, when I run the code it runs in the foreground, which means I can’t do anything until I kill it.

Here’s the Vimscript code:

" Automatic light mode / dark mode switcher
function! ChangeColorScheme(channel, msg)
  let time = trim(a:msg)
  if time ==# "day"
    call LightMode()
  else
    call DarkMode()
  endif
endfunction

function! Sunshine(timer)
  if executable("sunshine")
    " Add your desired location here instead of '@45 15' (I probably could have
    " made it into a variable)
    let job = job_start(["sunshine", "-s", "@45 15"], {"out_cb": "ChangeColorScheme"})
  else
    call DarkMode()
  endif
endfunction

function! AutoDarkModeSetup()
  let timer = timer_start(30000, 'Sunshine', {'repeat': -1})
  call Sunshine(timer) " Initial call to setup the theme
endfunction

call AutoDarkModeSetup()

and here’s my Lua code:

-- Automatic light mode / dark mode switcher
local loop = vim.loop
local cmd = vim.cmd
local stat = require("posix.sys.stat")
local socket = require ("socket")
local gettime = socket.gettime

function LightMode()
    cmd [[colorscheme github]]
end

function DarkMode()
    cmd [[colorscheme dracula]]
end

function trim(s)
   return (s:gsub("^%s*(.-)%s*$", "%1"))
end

function ChangeColorScheme(msg)
  local time = trim(msg)
  if time == "day" then
    return LightMode()
  else
    return DarkMode()
  end
end

function Sunshine()
  if stat["/usr/local/bin/sunshine"] then
    job = loop.spawn('sunshine', {
            args = {'-s', '@36 30'},
            stdio = {nil,stdout,stderr}
        },
        vim.schedule_wrap(function()
          stdout:read_stop()
          stderr:read_stop()
          stdout:close()
          stderr:close()
          job:close()
          setQF()
        end
        )
        )
        loop.read_start(stdout, ChangeColorScheme)
        loop.read_start(stderr, ChangeColorScheme)
  else
    return DarkMode()
  end
end

function AutoDarkModeSetup()
  local function timer(seconds)
    local exp_time = gettime() + seconds
    return function()
       if gettime() < exp_time then return false end
       exp_time = exp_time + seconds
       return true
    end
  end

  local t1 = timer(30) -- a timer that expires every 30 seconds

  while true do
     if t1() then Sunshine() end
  end
end

AutoDarkModeSetup()

I call it in my init.lua by simply adding a require().

I apologize if my question is too obvious, I have never coded in Lua before.

You can use timer of vim.loop like this:

local function time_background()
    local timer = vim.loop.new_timer()
    timer:start(0, 600, vim.schedule_wrap(function()
        local hour = tonumber(os.date('%H'))
        local bg = (hour > 6 and hour < 18) and 'light' or 'dark'
        if vim.o.bg ~= bg then vim.o.bg = bg end
    end))
end