@@ -51,7 +51,8 @@ https://www.ecma-international.org/publications/standards/Ecma-262.htm
51
51
REQUIREMENTS *Vital.Async.Promise-requirements*
52
52
53
53
| Vital.Async.Promise | requires | lambda | and | timers | features.
54
- So Vim 8.0 or later is required. Recent version of Neovim also supports them.
54
+ So Vim 8.0 or later is required. The recent version of Neovim also supports
55
+ them.
55
56
56
57
57
58
@@ -74,9 +75,9 @@ Before explaining the detail of APIs, let's see actual examples.
74
75
One of most simple asynchronous operation is a timer. It calls a specified
75
76
callback when exceeding the timeout. "s:Promise.new" creates a new Promise
76
77
object with given callback. In the callback, function "resolve" (and
77
- "reject" if needed) is passed. When asynchronous operation is done (in this
78
- case, when timer is expired), call "resolve" on success or call "reject" on
79
- failure.
78
+ "reject" if needed) is passed. When the asynchronous operation is done (in
79
+ this case, when the timer is expired), call "resolve" on success or call
80
+ "reject" on failure.
80
81
81
82
82
83
(2) Next tick *Vital.Async.Promise-example-next-tick*
@@ -92,8 +93,9 @@ Before explaining the detail of APIs, let's see actual examples.
92
93
\.catch({err -> execute('echom err', '')})
93
94
<
94
95
By giving 0 to | timer_start() | as timeout, it waits for "next tick". It's the
95
- first time when Vim waits input. It means that Vim gives higher priority to
96
- user input and executes the script (in callback of | timer_start() | ) after.
96
+ first time when Vim waits for input. It means that Vim gives higher priority
97
+ to user input and executes the script (in the callback of | timer_start() | )
98
+ after.
97
99
98
100
99
101
(3) Job *Vital.Async.Promise-example-job*
@@ -120,26 +122,26 @@ Before explaining the detail of APIs, let's see actual examples.
120
122
endfunction
121
123
<
122
124
| job | is a feature to run commands asynchronously. But it is a bit hard to use
123
- because it requires callback. By wrapping it with Promise, it makes further
124
- easier to use commands and handle errors asynchronously.
125
+ because it requires a callback. By wrapping it with Promise, it makes
126
+ further easier to use commands and handle errors asynchronously.
125
127
126
- s:read() is just a helper fnction which reads all output of channel from
127
- job. So it's not so important.
128
+ s:read() is just a helper function which reads all output of channel from
129
+ the job. So it's not so important.
128
130
129
131
Important part is "return ..." in s:sh(). It creates a Promise which starts
130
132
a job and resolves when the given command has done. It calls resolve() when
131
- the command finished successfully with output from stdout, and calls
132
- reject() when the command failed with output from stderr.
133
+ the command finished successfully with an output from stdout, and calls
134
+ reject() when the command failed with an output from stderr.
133
135
134
- "ls -l" can be executed as following :
136
+ "ls -l" can be executed as follows :
135
137
>
136
138
call s:sh('ls', '-l')
137
139
\.then({out -> execute('echo "Output: " . out', '')})
138
140
\.catch({err -> execute('echo "Error: " . err', '')})
139
141
<
140
- As more complex example, following code clones 4 repositories and shows a
141
- message when all of them has completed. When one of them fails, it shows an
142
- error message without waiting other operations.
142
+ As the more complex example, following code clones 4 repositories and shows
143
+ a message when all of them has completed. When one of them fails, it shows
144
+ an error message without waiting for other operations.
143
145
>
144
146
call s:Promise.all([
145
147
\ s:sh('git', 'clone', 'https://github.com/thinca/vim-quickrun.git'),
@@ -169,11 +171,11 @@ Before explaining the detail of APIs, let's see actual examples.
169
171
\})
170
172
<
171
173
s:sh() and s:wait() are explained above. And .race() awaits one of given
172
- Promise objects has finished.
174
+ Promise objects have finished.
173
175
174
176
The .race() awaits either s:sh(...) or s:wait(...) has completed or failed.
175
177
It means that it clones Vim repository from GitHub via git command, but if
176
- it exceeds 10 seconds, it does not wait the clone operation anymore.
178
+ it exceeds 10 seconds, it does not wait for the clone operation anymore.
177
179
178
180
By adding .then() and giving the result value (v:false or v:true here), you
179
181
can know whether the asynchronous operation was timed out or not in
@@ -223,10 +225,10 @@ new({executor}) *Vital.Async.Promise.new()*
223
225
224
226
{executor} is a | Funcref | which represents how to create a Promise
225
227
object. It is called _synchronously_. It receives two functions as
226
- parameters. First parameter is "resolve". It accepts one or zero
228
+ parameters. The first parameter is "resolve". It accepts one or zero
227
229
argument. By calling it in {executor} , new() returns a fulfilled
228
- Promise object. Second parameter is "reject". It also accepts one or
229
- zero argument. By calling it in {executor} , new() returns rejected
230
+ Promise object. The second parameter is "reject". It also accepts one
231
+ or zero argument. By calling it in {executor} , new() returns rejected
230
232
Promise object.
231
233
>
232
234
" Fulfilled Promise object with 42
@@ -236,9 +238,9 @@ new({executor}) *Vital.Async.Promise.new()*
236
238
let p = Promise.new({_, reject -> reject('ERROR!')})
237
239
let p = Promise.new({-> execute('throw "ERROR!"')})
238
240
<
239
- When other Promise object is passed to "resolve" or "reject" function
240
- call, new() returns a pending Promise object which awaits until the
241
- given other Promise object has finished.
241
+ When another Promise object is passed to "resolve" or "reject"
242
+ function call, new() returns a pending Promise object which awaits
243
+ until the given other Promise object has finished.
242
244
243
245
If an exception is thrown in {executor} , new() returns a rejected
244
246
Promise object with the exception.
@@ -285,16 +287,16 @@ reject([{value}]) *Vital.Async.Promise.reject()*
285
287
<
286
288
all({promises} ) *Vital.Async.Promise.all()*
287
289
288
- Creates a Promise object which awaits until all of {promises} has
289
- completed. It resolves the Promise object with a list of results of
290
- {promises} as following:
290
+ Creates a Promise object which awaits all of {promises} has completed.
291
+ It resolves the Promise object with a list of results of {promises} as
292
+ following:
291
293
>
292
294
call Promise.all([Promise.resolve(1), Promise.resolve('foo')])
293
295
\.then({arr -> execute('echo arr', '')})
294
296
" It shows [1, 'foo']
295
297
<
296
- If one of them is rejected, it does not await for other
297
- Promise objects and the Promise object is rejected immediately.
298
+ If one of them is rejected, it does not await other Promise objects
299
+ and the Promise object is rejected immediately.
298
300
299
301
>
300
302
call Promise.all([Promise.resolve(1), Promise.reject('ERROR!')])
@@ -404,8 +406,8 @@ asynchronous operation. It represents one of following states:
404
406
405
407
{promise} .catch([{onRejected} ]) *Vital.Async.Promise-Promise.catch()*
406
408
407
- It is a shortcut function of calling .then() where first argument is
408
- | v:null | .
409
+ It is a shortcut function of calling .then() where the first argument
410
+ is | v:null | .
409
411
>
410
412
" Followings are equal
411
413
call Promise.reject('ERROR').then(v:null, {msg -> execute('echo msg', '')})
@@ -416,7 +418,7 @@ Exception Object *Vital.Async.Promise-objects-Exception*
416
418
417
419
Exception object represents an exception of Vim script. Since Vim script's
418
420
| v:exception | is a | String | value and a stack trace of the exception is
419
- separated to | v:throwpoint | variable, it does not fit to Promise API.
421
+ separated to | v:throwpoint | variable, it does not fit Promise API.
420
422
So we need to define our own exception object. It is passed to {onRejected}
421
423
parameter of .then() or .catch() method.
422
424
@@ -433,8 +435,8 @@ Example:
433
435
" {'exception': 'Vim(return):E691: ...', 'throwpoint': '...'}
434
436
<
435
437
Exception object has two fields; "exception" and "throwpoint".
436
- "exception" is a error message. It's corresponding to | v:exception | . And
437
- "throwpoint" is a stack trace of caught exception. It's corresponding to
438
+ "exception" is an error message. It's corresponding to | v:exception | . And
439
+ "throwpoint" is a stack trace of the caught exception. It's corresponding to
438
440
| v:throwpoint | .
439
441
440
442
==============================================================================
0 commit comments