@@ -76,14 +76,14 @@ M.join = function(async_fns)
76
76
return results
77
77
end
78
78
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
81
81
--- @return ...
82
- M .run_first = a .wrap (function (async_funs , step )
82
+ M .run_first = a .wrap (function (async_functions , step )
83
83
local ran = false
84
84
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" )
87
87
88
88
local callback = function (...)
89
89
if not ran then
@@ -92,10 +92,22 @@ M.run_first = a.wrap(function(async_funs, step)
92
92
end
93
93
end
94
94
95
- future (callback )
95
+ async_function (callback )
96
96
end
97
97
end , 2 )
98
98
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
+
99
111
M .run_all = function (async_fns , callback )
100
112
a .run (function ()
101
113
M .join (async_fns )
0 commit comments