@@ -64,16 +64,20 @@ endfunction
64
64
" ... is added to use this function as a callback of timer_start()
65
65
function ! s: _publish (promise, ... ) abort
66
66
let settled = a: promise ._state
67
+ if settled == s: PENDING
68
+ throw ' vital: Async.Promise: Cannot publish a pending promise'
69
+ endif
70
+
67
71
if empty (a: promise ._children)
68
72
return
69
73
endif
74
+
70
75
for i in range (len (a: promise ._children))
71
76
if settled == s: FULFILLED
72
77
let l: CB = a: promise ._fulfillments[i ]
73
- elseif settled == s: REJECTED
74
- let l: CB = a: promise ._rejections[i ]
75
78
else
76
- throw ' vital: Async.Promise: Cannot publish a pending promise'
79
+ " When rejected
80
+ let l: CB = a: promise ._rejections[i ]
77
81
endif
78
82
let child = a: promise ._children[i ]
79
83
if type (child) != v: t_none
@@ -82,6 +86,7 @@ function! s:_publish(promise, ...) abort
82
86
call l: CB (a: promise ._result)
83
87
endif
84
88
endfor
89
+
85
90
let a: promise ._children = []
86
91
let a: promise ._fulfillments = []
87
92
let a: promise ._rejections = []
@@ -142,10 +147,10 @@ function! s:_reject(promise, ...) abort
142
147
endfunction
143
148
144
149
function ! s: _notify_done (wg, index , value) abort
145
- let a: wg .done [a: index ] = a: value
146
- let a: wg .resolved + = 1
147
- if a: wg .resolved == a: wg .total
148
- call a: wg .resolve (a: wg .done )
150
+ let a: wg .results [a: index ] = a: value
151
+ let a: wg .remaining - = 1
152
+ if a: wg .remaining == 0
153
+ call a: wg .resolve (a: wg .results )
149
154
endif
150
155
endfunction
151
156
@@ -157,10 +162,9 @@ function! s:_all(promises, resolve, reject) abort
157
162
endif
158
163
159
164
let wait_group = {
160
- \ ' done ' : repeat ([v: null ], total),
165
+ \ ' results ' : repeat ([v: null ], total),
161
166
\ ' resolve' : a: resolve ,
162
- \ ' resolved' : 0 ,
163
- \ ' total' : total,
167
+ \ ' remaining' : total,
164
168
\ }
165
169
166
170
" 'for' statement is not available here because iteration variable is captured into lambda
0 commit comments