Skip to content

Commit ae5e38c

Browse files
committed
Add Promise.on_unhandled_rejection
1 parent 4d1d157 commit ae5e38c

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

autoload/vital/__vital__/Async/Promise.vim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ function! s:_publish(promise, ...) abort
9090
endif
9191

9292
if empty(a:promise._children)
93+
if settled == s:REJECTED
94+
call s:_on_unhandled_rejection(a:promise._result)
95+
endif
9396
return
9497
endif
9598

@@ -270,6 +273,11 @@ function! s:wait(promise, ...) abort
270273
endif
271274
endfunction
272275

276+
let s:_on_unhandled_rejection = s:NOOP
277+
function! s:on_unhandled_rejection(on_unhandled_rejection) abort
278+
let s:_on_unhandled_rejection = a:on_unhandled_rejection
279+
endfunction
280+
273281
function! s:_promise_then(...) dict abort
274282
let parent = self
275283
let state = parent._state

doc/vital/Async/Promise.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,13 @@ wait({promise}[, {options}]) *Vital.Async.Promise.wait()*
377377
call Promise.wait(p, 1000)
378378
" Is equivalent to call Promise.wait(p, {'timeout': 1000})
379379
>
380+
on_unhandled_rejection({callback}) *Vital.Async.Promise.on_unhandled_rejection*
381+
382+
Set callback to catch all unhandled rejected promise's result.
383+
384+
Note:
385+
This callback will called even if you using |Vital.Async.Promise.wait()|.
386+
380387

381388
is_promise({value}) *Vital.Async.Promise.is_promise()*
382389

test/Async/Promise.vimspec

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,56 @@ Describe Async.Promise
804804
endif
805805
End
806806
End
807+
808+
Describe .on_unhandled_rejection
809+
It should call when promsie does not catch
810+
let l = l:
811+
call P.on_unhandled_rejection({ result -> extend(l, { 'result': result }) })
812+
813+
let p = P.resolve().then({ -> execute('throw "error"') })
814+
call s:wait_has_key(l, 'result')
815+
Assert HasKey(result, 'exception')
816+
Assert HasKey(result, 'throwpoint')
817+
End
818+
819+
It should call when promise does not catch with finally
820+
let l = l:
821+
call P.on_unhandled_rejection({ result -> extend(l, { 'result': result }) })
822+
823+
let p = P.resolve().then({ -> execute('throw "error"') }).finally({ -> {} })
824+
call s:wait_has_key(l, 'result')
825+
Assert HasKey(result, 'exception')
826+
Assert HasKey(result, 'throwpoint')
827+
End
828+
829+
It should call when promise does not catch with children
830+
let l = l:
831+
call P.on_unhandled_rejection({ result -> extend(l, { 'result': result }) })
832+
833+
let p = P.resolve().then({ -> execute('throw "error"') }).then({ -> {} })
834+
call s:wait_has_key(l, 'result')
835+
Assert HasKey(result, 'exception')
836+
Assert HasKey(result, 'throwpoint')
837+
End
838+
839+
It should call when promise does not catch with wait
840+
let l = l:
841+
call P.on_unhandled_rejection({ result -> extend(l, { 'result': result }) })
842+
843+
let p = P.resolve().then({ -> execute('throw "error"') }).then({ -> {} })
844+
let [_, error] = P.wait(p)
845+
Assert Equals(error, result)
846+
End
847+
848+
It should not call when promise catched
849+
let l = l:
850+
call P.on_unhandled_rejection({ result -> extend(l, { 'result': result }) })
851+
852+
let p = P.resolve().then({ -> execute('throw "error"') }).catch({ -> {} })
853+
call P.wait(Wait(100))
854+
Assert KeyNotExists(l, 'result')
855+
End
856+
End
807857
End
808858

809859
" vim:et ts=2 sts=2 sw=2 tw=0:

0 commit comments

Comments
 (0)