Skip to content

Commit 10ded34

Browse files
chore: Update Promise util
1 parent 61bddf7 commit 10ded34

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

lua/orgmode/utils/promise.lua

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function PackedValue.first(self)
2727
return first
2828
end
2929

30-
---@class Promise
30+
--- @class Promise
3131
local Promise = {}
3232
Promise.__index = Promise
3333

@@ -73,14 +73,25 @@ local new_pending = function(on_fullfilled, on_rejected)
7373
end
7474

7575
--- Equivalents to JavaScript's Promise.new.
76-
--- @param executor function: `(resolve: function(...), reject: function(...))`
77-
--- @return table: Promise
76+
--- @param executor fun(resolve:fun(...:any),reject:fun(...:any))
77+
--- @return Promise
7878
function Promise.new(executor)
7979
vim.validate({ executor = { executor, 'function' } })
8080

8181
local self = new_pending()
8282

8383
local resolve = function(...)
84+
local first = ...
85+
if is_promise(first) then
86+
first
87+
:next(function(...)
88+
self:_resolve(...)
89+
end)
90+
:catch(function(...)
91+
self:_reject(...)
92+
end)
93+
return
94+
end
8495
self:_resolve(...)
8596
end
8697
local reject = function(...)
@@ -93,8 +104,8 @@ end
93104

94105
--- Returns a fulfilled promise.
95106
--- But if the first argument is promise, returns the promise.
96-
--- @vararg any: one promise or non-promises
97-
--- @return table: Promise
107+
--- @param ... any: one promise or non-promises
108+
--- @return Promise
98109
function Promise.resolve(...)
99110
local first = ...
100111
if is_promise(first) then
@@ -108,8 +119,8 @@ end
108119

109120
--- Returns a rejected promise.
110121
--- But if the first argument is promise, returns the promise.
111-
--- @vararg any: one promise or non-promises
112-
--- @return table: Promise
122+
--- @param ... any: one promise or non-promises
123+
--- @return Promise
113124
function Promise.reject(...)
114125
local first = ...
115126
if is_promise(first) then
@@ -201,9 +212,9 @@ function Promise._start_reject(self, value)
201212
end
202213

203214
--- Equivalents to JavaScript's Promise.then.
204-
--- @param on_fullfilled function|nil: A callback on fullfilled.
205-
--- @param on_rejected function|nil: A callback on rejected.
206-
--- @return table: Promise
215+
--- @param on_fullfilled (fun(...:any):any)?: A callback on fullfilled.
216+
--- @param on_rejected (fun(...:any):any)?: A callback on rejected.
217+
--- @return Promise
207218
function Promise.next(self, on_fullfilled, on_rejected)
208219
vim.validate({
209220
on_fullfilled = { on_fullfilled, 'function', true },
@@ -223,15 +234,15 @@ function Promise.next(self, on_fullfilled, on_rejected)
223234
end
224235

225236
--- Equivalents to JavaScript's Promise.catch.
226-
--- @param on_rejected function|nil: A callback on rejected.
227-
--- @return table: Promise
237+
--- @param on_rejected (fun(...:any):any)?: A callback on rejected.
238+
--- @return Promise
228239
function Promise.catch(self, on_rejected)
229240
return self:next(nil, on_rejected)
230241
end
231242

232243
--- Equivalents to JavaScript's Promise.finally.
233-
--- @param on_finally function
234-
--- @return table: Promise
244+
--- @param on_finally fun()
245+
--- @return Promise
235246
function Promise.finally(self, on_finally)
236247
vim.validate({ on_finally = { on_finally, 'function', true } })
237248
return self
@@ -247,8 +258,8 @@ end
247258

248259
--- Equivalents to JavaScript's Promise.all.
249260
--- Even if multiple value are resolved, results include only the first value.
250-
--- @param list table: promise or non-promise values
251-
--- @return table: Promise
261+
--- @param list any[]: promise or non-promise values
262+
--- @return Promise
252263
function Promise.all(list)
253264
vim.validate({ list = { list, 'table' } })
254265
return Promise.new(function(resolve, reject)
@@ -276,8 +287,8 @@ function Promise.all(list)
276287
end
277288

278289
--- Equivalents to JavaScript's Promise.race.
279-
--- @param list table: promise or non-promise values
280-
--- @return table: Promise
290+
--- @param list any[]: promise or non-promise values
291+
--- @return Promise
281292
function Promise.race(list)
282293
vim.validate({ list = { list, 'table' } })
283294
return Promise.new(function(resolve, reject)
@@ -295,8 +306,8 @@ end
295306

296307
--- Equivalents to JavaScript's Promise.any.
297308
--- Even if multiple value are rejected, errors include only the first value.
298-
--- @param list table: promise or non-promise values
299-
--- @return table: Promise
309+
--- @param list any[]: promise or non-promise values
310+
--- @return Promise
300311
function Promise.any(list)
301312
vim.validate({ list = { list, 'table' } })
302313
return Promise.new(function(resolve, reject)
@@ -325,8 +336,8 @@ end
325336

326337
--- Equivalents to JavaScript's Promise.allSettled.
327338
--- Even if multiple value are resolved/rejected, value/reason is only the first value.
328-
--- @param list table: promise or non-promise values
329-
--- @return table: Promise
339+
--- @param list any[]: promise or non-promise values
340+
--- @return Promise
330341
function Promise.all_settled(list)
331342
vim.validate({ list = { list, 'table' } })
332343
return Promise.new(function(resolve)

0 commit comments

Comments
 (0)