@@ -76,14 +76,14 @@ M.join = function(async_fns)
7676 return results
7777end
7878
79- --- Returns a future that when run will select the first async_function that finishes
80- --- @param async_funs table : The async_function that you want to select
79+ --- Returns a result from the future that finishes at the first
80+ --- @param async_functions table : The futures that you want to select
8181--- @return ...
82- M .run_first = a .wrap (function (async_funs , step )
82+ M .run_first = a .wrap (function (async_functions , step )
8383 local ran = false
8484
85- for _ , future in ipairs (async_funs ) do
86- assert (type (future ) == " function" , " type error :: future must be function" )
85+ for _ , async_function in ipairs (async_functions ) do
86+ assert (type (async_function ) == " function" , " type error :: future must be function" )
8787
8888 local callback = function (...)
8989 if not ran then
@@ -92,10 +92,22 @@ M.run_first = a.wrap(function(async_funs, step)
9292 end
9393 end
9494
95- future (callback )
95+ async_function (callback )
9696 end
9797end , 2 )
9898
99+ --- Returns a result from the functions that finishes at the first
100+ --- @param funcs table : The async functions that you want to select
101+ --- @return ...
102+ M .race = function (funcs )
103+ local async_functions = vim .tbl_map (function (func )
104+ return function (callback )
105+ a .run (func , callback )
106+ end
107+ end , funcs )
108+ return M .run_first (async_functions )
109+ end
110+
99111M .run_all = function (async_fns , callback )
100112 a .run (function ()
101113 M .join (async_fns )
0 commit comments