-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync-ls.ls
61 lines (51 loc) · 1.99 KB
/
async-ls.ls
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
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!
returnP result
p.catch Promise.CancellationError, (e) ->
p = f!
..finally -> g!
throw (new CancellationError p)
# bindP :: (CancellablePromise cp) => cp a -> (a -> cp b) -> cp b
bindP = (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!
# returnP :: (CancellablePromise cp) => a -> cp a
returnP = (a) -> new-promise (res) -> res a
# rejectP :: (CancellablePromise cp) => a -> cp a
rejectP = (a) -> new-promise (, rej) -> rej 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
# sequenceP :: (CancellablePromise cp) => [cp a] -> cp [a]
sequenceP = ([p, ...ps]) ->
return returnP p if !p
a <- bindP p
as <- bindP (sequenceP ps)
[a] ++ as
module.exports = {with-cancel-and-dispose, bindP, returnP, rejectP, from-error-value-callback, to-callback, new-promise, sequenceP}