Closed jcubic closed 3 years ago
I'm not sure what the problem is, this works fine:
lips> (define (foo arr)
(let ((x (new Promise (lambda (resolve)
(setTimeout (lambda ()
(print "x")
(resolve arr))
1000)))))
(--> x (forEach (lambda (x) (print x))))))
lips> (begin
(foo (list->vector (range 10)))
(print "done")
(foo (list->vector (range 10)))
(print "done"))
x
0
1
2
3
4
5
6
7
8
9
done
x
0
1
2
3
4
5
6
7
8
9
done
The problem was that JavaScript forEach don't return a value. Here is use case:
(define (p time value)
(new Promise (lambda (resolve)
(setTimeout (lambda ()
(resolve value))
time))))
(define (foo arr)
(let ((x (p 1000 arr)))
(--> x (forEach (lambda (x)
(if (p 100 #t)
(print x)))))))
(begin
(foo (list->vector (range 10)))
(print "done")
(foo (list->vector (range 10)))
(print "done"))
This doesn't work, but if you change forEach with a map it works fine and expressions are in sync. The reason why the original code was not working was that bound?
was returning a promise that was ignored.
This requires documentation.
The case documented at Wiki 1.0 Draft
This make calling two function not in sync they are called in parallel:
just wrapping with promise fix the issue.
The same happen in browser with
fetch
. Promise resolving mechanism may require rethinking.