@@ -332,6 +332,53 @@ function Promise.all(list)
332
332
end )
333
333
end
334
334
335
+ --- Equivalents to JavaScript's Promise.map with concurrency limit.
336
+ --- @param callback fun ( value : any , index : number , array : any[] ): any
337
+ --- @param list any[] : promise or non-promise values
338
+ --- @param concurrency ? number : limit number of concurrent items processing
339
+ --- @return OrgPromise
340
+ function Promise .map (callback , list , concurrency )
341
+ vim .validate ({
342
+ list = { list , ' table' },
343
+ callback = { callback , ' function' },
344
+ concurrency = { concurrency , ' number' , true },
345
+ })
346
+
347
+ local results = {}
348
+ local processing = 0
349
+ local index = 1
350
+ concurrency = concurrency or # list
351
+
352
+ return Promise .new (function (resolve , reject )
353
+ local function processNext ()
354
+ if index > # list then
355
+ if processing == 0 then
356
+ resolve (results )
357
+ end
358
+ return
359
+ end
360
+
361
+ local i = index
362
+ index = index + 1
363
+ processing = processing + 1
364
+
365
+ Promise .resolve (callback (list [i ], i , list ))
366
+ :next (function (...)
367
+ results [i ] = ...
368
+ processing = processing - 1
369
+ processNext ()
370
+ end )
371
+ :catch (function (...)
372
+ reject (... )
373
+ end )
374
+ end
375
+
376
+ for _ = 1 , concurrency do
377
+ processNext ()
378
+ end
379
+ end )
380
+ end
381
+
335
382
--- Equivalents to JavaScript's Promise.race.
336
383
--- @param list any[] : promise or non-promise values
337
384
--- @return OrgPromise
0 commit comments