Skip to content

Commit 05e02b4

Browse files
committed
Async.Promise: Give tags to examples and fix example codes
Thank you @tyru and @haya14busa!
1 parent bfdb043 commit 05e02b4

File tree

1 file changed

+46
-45
lines changed

1 file changed

+46
-45
lines changed

Diff for: doc/vital/Async/Promise.txt

+46-45
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ EXAMPLE *Vital.Async.Promise-example*
5757

5858
Before explaining the detail of APIs, let's see actual examples.
5959

60-
(1) Timer
60+
(1) Timer *Vital.Async.Promise-example-timer*
6161
>
6262
let s:Promise = vital#vital#import('Async.Promise')
6363
6464
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)})
6666
endfunction
6767
6868
call s:wait(500).then({-> execute('echo "After 500ms"', '')})
@@ -76,22 +76,22 @@ Before explaining the detail of APIs, let's see actual examples.
7676
failure.
7777

7878

79-
(2) Next tick
79+
(2) Next tick *Vital.Async.Promise-example-next-tick*
8080
>
8181
function! s:next_tick()
8282
return s:Promise.new({resolve -> timer_start(0, resolve)})
8383
endfunction
8484
8585
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', '')})
8888
<
8989
By giving 0 to |timer_start()| as timeout, it waits for "next tick". It's the
9090
first time when Vim waits input. It means that Vim gives higher priority to
9191
user input and executes the script (in callback of |timer_start()|) after.
9292

9393

94-
(3) Job
94+
(3) Job *Vital.Async.Promise-example-job*
9595
>
9696
let s:Promise = vital#vital#import('Async.Promise')
9797
@@ -137,7 +137,6 @@ Before explaining the detail of APIs, let's see actual examples.
137137
As more complex example, following code clones 4 repositories and shows a
138138
message when all of them has completed. When one of them fails, it shows an
139139
error message without waiting other operations.
140-
141140
>
142141
call s:Promise.all([
143142
\ 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.
146145
\ s:sh('git', 'clone', 'https://github.com/rhysd/clever-f.vim.git'),
147146
\]
148147
\)
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', '')})
151150
<
152151
s:Promise.all(...) awaits all given promises have completed, or one of them
153152
has failed.
154153

155154

156-
(4) Timeout
155+
(4) Timeout *Vital.Async.Promise-example-timeout*
157156

158157
Let's see how Promise realizes timeout easily.
159-
160158
>
161159
call s:Promise.race([
162160
\ s:sh('git', 'clone', 'https://github.com/vim/vim.git').then({-> v:false}),
163161
\ 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+
\})
165165
<
166166
s:sh() and s:wait() are explained above. And .race() awaits one of given
167167
Promise objects has finished.
@@ -175,7 +175,7 @@ Before explaining the detail of APIs, let's see actual examples.
175175
succeeding .then() method. The parameter "timed_out" represents it.
176176

177177

178-
(5) REST API call
178+
(5) REST API call *Vital.Async.Promise-example-rest-api*
179179

180180
At last, let's see how Promise handles API call with |job| and curl
181181
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.
189189
return s:sh('curl', url)
190190
\.then({data -> json_decode(data)})
191191
\.then({res -> has_key(res, 'items') ?
192-
\ items :
192+
\ res.items :
193193
\ execute('throw ' . string(res.message))})
194194
endfunction
195195
196196
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', '')})
199199
<
200200
In this example, it searches the issue in Vim repository on GitHub which
201201
gained the most :+1: reactions.
202202

203203
In s:github_issues(), it calls GitHub Issue Search API using curl command
204204
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
206206
returned failure response, the Promise value will be rejected. The rejection
207207
will be caught in .catch() method at the last line and an error message will
208208
be shown.
@@ -225,11 +225,11 @@ new({executor}) *Vital.Async.Promise.new()*
225225
Promise object.
226226
>
227227
" Fulfilled Promise object with 42
228-
Promise.new({resolve -> resolve(42)})
228+
let p = Promise.new({resolve -> resolve(42)})
229229
230230
" 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!"')})
233233
<
234234
When other Promise object is passed to "resolve" or "reject" function
235235
call, new() returns a pending Promise object which awaits until the
@@ -243,8 +243,9 @@ new({executor}) *Vital.Async.Promise.new()*
243243
If "resolve" or "reject" is called with no argument, it resolves a
244244
Promise object with |v:null|.
245245
>
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', '')})
248249
<
249250
resolve([{value}]) *Vital.Async.Promise.resolve()*
250251

@@ -253,18 +254,18 @@ resolve([{value}]) *Vital.Async.Promise.resolve()*
253254
new():
254255
>
255256
" 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)})
258259
<
259260
If {value} is a Promise object, it resolves/rejects with a value which
260261
given Promise object resolves/rejects with.
261262
>
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', '')})
264265
" Outputs '42'
265266
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', '')})
268269
" Outputs 'ERROR!'
269270
<
270271
reject([{value}]) *Vital.Async.Promise.reject()*
@@ -274,25 +275,25 @@ reject([{value}]) *Vital.Async.Promise.reject()*
274275
new():
275276
>
276277
" 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!')})
279280
<
280281
all({promises}) *Vital.Async.Promise.all()*
281282

282283
Creates a Promise object which awaits until all of {promises} has
283284
completed. It resolves the Promise object with an array of results of
284285
{promises} as following:
285286
>
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', '')})
288289
" It shows [1, 'foo']
289290
<
290291
If one of them is rejected, it does not await for other
291292
Promise objects and the Promise object is rejected immediately.
292293

293294
>
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', '')})
296297
" It shows 'ERROR!'
297298
<
298299
If an empty list is given, it is equivalent to Promise.resolve([]).
@@ -302,19 +303,19 @@ race({promises}) *Vital.Async.Promise.race()*
302303
Creates a Promise object which resolves or rejects as soon as one of
303304
{promises} resolves or rejects.
304305
>
305-
Promise.race([
306+
call Promise.race([
306307
\ Promise.new({resolve -> timer_start(50, {-> resolve('first')})}),
307308
\ Promise.new({resolve -> timer_start(100, {-> resolve('second')})}),
308309
\])
309-
\.then({v -> execute('echo ' . v)})
310+
\.then({v -> execute('echo v', '')})
310311
" It outputs 'first'
311312
312-
Promise.race([
313+
call Promise.race([
313314
\ Promise.new({resolve -> timer_start(50, {-> execute('throw "ERROR!"')})}),
314315
\ Promise.new({resolve -> timer_start(100, {-> resolve('second')})}),
315316
\])
316-
\.then({v -> execute('echo ' . v)})
317-
\.catch({e -> execute('echo ' . e)})
317+
\.then({v -> execute('echo v', '')})
318+
\.catch({e -> execute('echo e', '')})
318319
" It outputs 'ERROR!'
319320
<
320321
If {promises} is an empty list, the returned Promise object will never
@@ -358,8 +359,8 @@ asynchronous operation. It represents one of following states:
358359
be |Funcref| and they are guaranteed to be called __asynchronously__.
359360
>
360361
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"', '')})
363364
echo 'yo'
364365
<
365366
Above script following following:
@@ -386,8 +387,8 @@ asynchronous operation. It represents one of following states:
386387
exception.
387388
>
388389
" 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!')})
391392
<
392393
{onResolved} and {onRejected} can be |v:null|.
393394

@@ -397,8 +398,8 @@ asynchronous operation. It represents one of following states:
397398
|v:null|.
398399
>
399400
" 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', '')})
402403
<
403404
==============================================================================
404405
vim:tw=78:fo=tcq2mM:ts=8:ft=help:norl

0 commit comments

Comments
 (0)