@@ -57,12 +57,12 @@ EXAMPLE *Vital.Async.Promise-example*
57
57
58
58
Before explaining the detail of APIs, let's see actual examples.
59
59
60
- (1) Timer
60
+ (1) Timer *Vital.Async.Promise-example-timer*
61
61
>
62
62
let s:Promise = vital#vital#import('Async.Promise')
63
63
64
64
function! s:wait(ms)
65
- return s:Promise.new({resolve -> timer_start(a:ms)})
65
+ return s:Promise.new({resolve -> timer_start(a:ms, resolve )})
66
66
endfunction
67
67
68
68
call s:wait(500).then({-> execute('echo "After 500ms"', '')})
@@ -76,22 +76,22 @@ Before explaining the detail of APIs, let's see actual examples.
76
76
failure.
77
77
78
78
79
- (2) Next tick
79
+ (2) Next tick *Vital.Async.Promise-example-next-tick*
80
80
>
81
81
function! s:next_tick()
82
82
return s:Promise.new({resolve -> timer_start(0, resolve)})
83
83
endfunction
84
84
85
85
call s:next_tick()
86
- \.then({-> 'Execute lower priority tasks'})
87
- \.catch({err -> execute('echom ' . string( err) , '')})
86
+ \.then({-> 'Execute lower priority tasks here '})
87
+ \.catch({err -> execute('echom err' , '')})
88
88
<
89
89
By giving 0 to | timer_start() | as timeout, it waits for "next tick". It's the
90
90
first time when Vim waits input. It means that Vim gives higher priority to
91
91
user input and executes the script (in callback of | timer_start() | ) after.
92
92
93
93
94
- (3) Job
94
+ (3) Job *Vital.Async.Promise-example-job*
95
95
>
96
96
let s:Promise = vital#vital#import('Async.Promise')
97
97
@@ -137,7 +137,6 @@ Before explaining the detail of APIs, let's see actual examples.
137
137
As more complex example, following code clones 4 repositories and shows a
138
138
message when all of them has completed. When one of them fails, it shows an
139
139
error message without waiting other operations.
140
-
141
140
>
142
141
call s:Promise.all([
143
142
\ s:sh('git', 'clone', 'https://github.com/thinca/vim-quickrun.git'),
@@ -146,22 +145,23 @@ Before explaining the detail of APIs, let's see actual examples.
146
145
\ s:sh('git', 'clone', 'https://github.com/rhysd/clever-f.vim.git'),
147
146
\]
148
147
\)
149
- \.then({-> exeute ('echom "All repositories were successfully cloned!"', '')})
150
- \.catch({err -> execute('echom "Failed to clone: " . ' string( err) , '')})
148
+ \.then({-> execute ('echom "All repositories were successfully cloned!"', '')})
149
+ \.catch({err -> execute('echom "Failed to clone: " . err' , '')})
151
150
<
152
151
s:Promise.all(...) awaits all given promises have completed, or one of them
153
152
has failed.
154
153
155
154
156
- (4) Timeout
155
+ (4) Timeout *Vital.Async.Promise-example-timeout*
157
156
158
157
Let's see how Promise realizes timeout easily.
159
-
160
158
>
161
159
call s:Promise.race([
162
160
\ s:sh('git', 'clone', 'https://github.com/vim/vim.git').then({-> v:false}),
163
161
\ s:wait(10000).then({-> v:true}),
164
- \]).then({timed_out -> execute(timed_out ? 'Timeout!' : 'Cloned!', '')})
162
+ \]).then({timed_out ->
163
+ \ execute('echom timed_out ? "Timeout!" : "Cloned!"', '')
164
+ \})
165
165
<
166
166
s:sh() and s:wait() are explained above. And .race() awaits one of given
167
167
Promise objects has finished.
@@ -175,7 +175,7 @@ Before explaining the detail of APIs, let's see actual examples.
175
175
succeeding .then() method. The parameter "timed_out" represents it.
176
176
177
177
178
- (5) REST API call
178
+ (5) REST API call *Vital.Async.Promise-example-rest-api*
179
179
180
180
At last, let's see how Promise handles API call with | job | and curl
181
181
command. Here, we utilize encodeURIComponent() function in | Vital.Web.HTTP |
@@ -189,20 +189,20 @@ Before explaining the detail of APIs, let's see actual examples.
189
189
return s:sh('curl', url)
190
190
\.then({data -> json_decode(data)})
191
191
\.then({res -> has_key(res, 'items') ?
192
- \ items :
192
+ \ res. items :
193
193
\ execute('throw ' . string(res.message))})
194
194
endfunction
195
195
196
196
call s:github_issues('repo:vim/vim sort:reactions-+1')
197
- \.then({issues -> execute('echom ' . string( issues[0].url) , '')})
198
- \.catch({err -> execute('echom ' . string(' ERROR: ' . err) , '')})
197
+ \.then({issues -> execute('echom issues[0].url' , '')})
198
+ \.catch({err -> execute('echom " ERROR: " . err' , '')})
199
199
<
200
200
In this example, it searches the issue in Vim repository on GitHub which
201
201
gained the most :+1: reactions.
202
202
203
203
In s:github_issues(), it calls GitHub Issue Search API using curl command
204
204
and s:sh() function explained above. And it decodes the returned JSON by
205
- | json_decode | and checks the content. If the curl command failed or API
205
+ | json_decode() | and checks the content. If the curl command failed or API
206
206
returned failure response, the Promise value will be rejected. The rejection
207
207
will be caught in .catch() method at the last line and an error message will
208
208
be shown.
@@ -225,11 +225,11 @@ new({executor}) *Vital.Async.Promise.new()*
225
225
Promise object.
226
226
>
227
227
" Fulfilled Promise object with 42
228
- Promise.new({resolve -> resolve(42)})
228
+ let p = Promise.new({resolve -> resolve(42)})
229
229
230
230
" Rejected Promise object with 'ERROR!'
231
- Promise.new({_, reject -> reject('ERROR!')})
232
- Promise.new({-> execute('throw "ERROR!"')})
231
+ let p = Promise.new({_, reject -> reject('ERROR!')})
232
+ let p = Promise.new({-> execute('throw "ERROR!"')})
233
233
<
234
234
When other Promise object is passed to "resolve" or "reject" function
235
235
call, new() returns a pending Promise object which awaits until the
@@ -243,8 +243,9 @@ new({executor}) *Vital.Async.Promise.new()*
243
243
If "resolve" or "reject" is called with no argument, it resolves a
244
244
Promise object with | v:null | .
245
245
>
246
- Promise.new({resolve -> resolve()}).then({x -> execute('echo ' . x)})
247
- " It outputs 'v:null'
246
+ " :echo outputs 'v:null'
247
+ Promise.new({resolve -> resolve()})
248
+ \.then({x -> execute('echo x', '')})
248
249
<
249
250
resolve([{value} ]) *Vital.Async.Promise.resolve()*
250
251
@@ -253,18 +254,18 @@ resolve([{value}]) *Vital.Async.Promise.resolve()*
253
254
new():
254
255
>
255
256
" Followings are equivalent
256
- Promise.resolve(42)
257
- Promise.new({resolve -> resolve(42)})
257
+ let p = Promise.resolve(42)
258
+ let p = Promise.new({resolve -> resolve(42)})
258
259
<
259
260
If {value} is a Promise object, it resolves/rejects with a value which
260
261
given Promise object resolves/rejects with.
261
262
>
262
- Promise.resolve(Promise.resolve(42))
263
- \.then({x -> execute('echo ' . x )})
263
+ call Promise.resolve(Promise.resolve(42))
264
+ \.then({x -> execute('echo x', '' )})
264
265
" Outputs '42'
265
266
266
- Promise.resolve(Promise.reject('ERROR!'))
267
- \.catch({reason -> execute('echo ' . reason )})
267
+ call Promise.resolve(Promise.reject('ERROR!'))
268
+ \.catch({reason -> execute('echo reason', '' )})
268
269
" Outputs 'ERROR!'
269
270
<
270
271
reject([{value} ]) *Vital.Async.Promise.reject()*
@@ -274,25 +275,25 @@ reject([{value}]) *Vital.Async.Promise.reject()*
274
275
new():
275
276
>
276
277
" Followings are equivalent
277
- Promise.reject('Rejected!')
278
- Promise.new({_, reject -> reject('Rejected!')})
278
+ let p = Promise.reject('Rejected!')
279
+ let p = Promise.new({_, reject -> reject('Rejected!')})
279
280
<
280
281
all({promises} ) *Vital.Async.Promise.all()*
281
282
282
283
Creates a Promise object which awaits until all of {promises} has
283
284
completed. It resolves the Promise object with an array of results of
284
285
{promises} as following:
285
286
>
286
- Promise.all([Promise.resolve(1), Promise.resolve('foo')])
287
- \.then({arr -> execute('echo ' . string(arr) )})
287
+ call Promise.all([Promise.resolve(1), Promise.resolve('foo')])
288
+ \.then({arr -> execute('echo arr', '' )})
288
289
" It shows [1, 'foo']
289
290
<
290
291
If one of them is rejected, it does not await for other
291
292
Promise objects and the Promise object is rejected immediately.
292
293
293
294
>
294
- Promise.all([Promise.resolve(1), Promise.reject('ERROR!')])
295
- \.catch({err -> execute('echo ' . string(err) )})
295
+ call Promise.all([Promise.resolve(1), Promise.reject('ERROR!')])
296
+ \.catch({err -> execute('echo err', '' )})
296
297
" It shows 'ERROR!'
297
298
<
298
299
If an empty list is given, it is equivalent to Promise.resolve([]).
@@ -302,19 +303,19 @@ race({promises}) *Vital.Async.Promise.race()*
302
303
Creates a Promise object which resolves or rejects as soon as one of
303
304
{promises} resolves or rejects.
304
305
>
305
- Promise.race([
306
+ call Promise.race([
306
307
\ Promise.new({resolve -> timer_start(50, {-> resolve('first')})}),
307
308
\ Promise.new({resolve -> timer_start(100, {-> resolve('second')})}),
308
309
\])
309
- \.then({v -> execute('echo ' . v )})
310
+ \.then({v -> execute('echo v', '' )})
310
311
" It outputs 'first'
311
312
312
- Promise.race([
313
+ call Promise.race([
313
314
\ Promise.new({resolve -> timer_start(50, {-> execute('throw "ERROR!"')})}),
314
315
\ Promise.new({resolve -> timer_start(100, {-> resolve('second')})}),
315
316
\])
316
- \.then({v -> execute('echo ' . v )})
317
- \.catch({e -> execute('echo ' . e )})
317
+ \.then({v -> execute('echo v', '' )})
318
+ \.catch({e -> execute('echo e', '' )})
318
319
" It outputs 'ERROR!'
319
320
<
320
321
If {promises} is an empty list, the returned Promise object will never
@@ -358,8 +359,8 @@ asynchronous operation. It represents one of following states:
358
359
be | Funcref | and they are guaranteed to be called __asynchronously__.
359
360
>
360
361
echo 'hi'
361
- Promise.new({resolve -> execute('echo "halo"') || resolve(42)})
362
- \.then({-> execute('echo "bye"')}, {-> execute('echo "ah"')})
362
+ call Promise.new({resolve -> execute('echo "halo"', ' ') || resolve(42)})
363
+ \.then({-> execute('echo "bye"', '' )}, {-> execute('echo "ah"', ' ')})
363
364
echo 'yo'
364
365
<
365
366
Above script following following:
@@ -386,8 +387,8 @@ asynchronous operation. It represents one of following states:
386
387
exception.
387
388
>
388
389
" Both followings create a rejected Promise value asynchronously
389
- Promise.resolve(42).then({-> execute('throw "ERROR!"')})
390
- Promise.resolve(42).then({-> Promise.reject('ERROR!')})
390
+ call Promise.resolve(42).then({-> execute('throw "ERROR!"')})
391
+ call Promise.resolve(42).then({-> Promise.reject('ERROR!')})
391
392
<
392
393
{onResolved} and {onRejected} can be | v:null | .
393
394
@@ -397,8 +398,8 @@ asynchronous operation. It represents one of following states:
397
398
| v:null | .
398
399
>
399
400
" Followings are equal
400
- Promise.reject('ERROR').then(v:null, {e -> execute('echo ' . e )})
401
- Promise.reject('ERROR').catch({e -> execute('echo ' . e )})
401
+ call Promise.reject('ERROR').then(v:null, {e -> execute('echo e', '' )})
402
+ call Promise.reject('ERROR').catch({e -> execute('echo e', '' )})
402
403
<
403
404
==============================================================================
404
405
vim:tw=78:fo=tcq2mM:ts=8:ft=help:norl
0 commit comments