-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathasync-ls.ls
More file actions
67 lines (58 loc) · 1.94 KB
/
async-ls.ls
File metadata and controls
67 lines (58 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Promise = require \bluebird
CancellationError = ((@message) !-> @name = \CancellationError)
..prototype = Error.prototype
# with-cancel-and-dispose :: (CancellablePromise cp) => cp a -> (() -> p b) -> (() -> Void) -> cp a
with-cancel-and-dispose = (p, f, g = (->)) ->
p.then (result) ->
g!
return-p result
p.catch Promise.CancellationError, (e) ->
p = f!
..finally -> g!
throw (new CancellationError p)
# bind-p :: (CancellablePromise cp) => cp a -> (a -> cp b) -> cp b
bind-p = (p, f) -> p.then (a) -> f a
# new-promise :: (CancellablePromise cp) => ((x -> Void) -> (Error -> Void) -> Void) -> cp x
new-promise = (callback) -> new Promise ((res, rej) -> callback res, rej) .cancellable!
# return-p :: (CancellablePromise cp) => a -> cp a
return-p = (a) -> new-promise (res) -> res a
# from-error-value-callback :: ((Error, result) -> void, Object?) -> CancellablePromise result
from-error-value-callback = (f, self = null) ->
(...args) ->
_res = null
_rej = null
args = args ++ [(error, result) ->
return _rej error if !!error
_res result
]
(res, rej) <- new-promise
_res := res
_rej := rej
try
f.apply self, args
catch ex
rej ex
# to-callback :: (CancellablePromise cp) => cp x -> CB x -> Void
to-callback = (p, callback) !-->
p.then ->
callback null, it
p.catch (err) ->
return (callback err, null) if err?.name != \CancellationError
err, result <- to-callback err?.message
callback (err or result), null
# sequence-p :: (CancellablePromise cp) => [cp a] -> cp [a]
sequence-p = ([p, ...ps]) ->
return return-p [] if !p
a <- bind-p p
as <- bind-p (sequence-p ps)
[a] ++ as
module.exports = {
Promise
bind-p
from-error-value-callback
new-promise
return-p
sequence-p
to-callback
with-cancel-and-dispose
}