Package Promise - lisp (JSCL) wrap for JavaScript Promise.
It is intended for use in the Moren environment.
Development
(lores:qload :promise :storejs t)
(lores:modlink :promise "prom.js")
(setq p1 (promise:make
(lambda (res rej)
(#j:setTimeout
(lambda () (case (random 2)
(0 (funcall rej "no"))
(1 (funcall res "yes")))) 100)))
(promise:is-promise p1)
;;=> t
(promise:then p1
(lambda (x) (print (list 'accepted x)))
(lambda (x) (print (list 'rejected x)))))
(promise:_catch
(promise:then-resolve p1 (lambda (x) (print (list 'accepted x))))
(lambda (x) (print (list 'rejected x))))
(promise:then-resolve (promise:all (promise:resolve 1) (promise:resolve 2)))
For example we use pify.
(setf #j:pify (require "pify"))
(defun somefn (arg1 arg2 callback &rest other)
;; some code
(if flag
(funcall callback nil arg1) ;; its (resolve arg1)
(funcall callback arg2 nil)) ;; its (reject arg2)
))
(promise:then (funcall (#j:pify #'somefn) 123 456)
(lambda (x) (print x)) ;; => resolve 123
(lambda (x) (print x))) ;; => rejected 456
Copyright © 2017,2018 Vladimir Mezentsev
GNU General Public License v3.0