mark-hahn / fjs

FORTH-like language for Javascript / Node
MIT License
50 stars 4 forks source link

parallelism #2

Closed dominictarr closed 9 years ago

dominictarr commented 11 years ago

hi, I have been curious about forth for a long time, and am very interested to see what you are working on here:

I have some questions about how cbs are handled.

how would you translate something like this to fjs?

//perform some async call for each property of obj
var n = 0
for (var k in obj) {
   n++
   async(k, next)
}
//if obj was empty, just continue
if(!n) n=1, next()

//print done when next is called n times.
function next () {
  if(--n) return
  console.log('done')
}

reading your documentation, it sounds like each cb would fork the stack in fjs, is there a way to rejoin these forks back together?

mark-hahn commented 11 years ago

All the forks join automatically and the wait only continues after all are done. Well, they don't actually join, the results are joined (which is the same thing since everything else is cloned).

The forks only stay separate when a cb is called again after a previous call allowed the wait to continue. This is how the http server example works. In other words if the fjs cb word is executed once, and the async code calls the cb more than once, then the wait will continue more than once leaving the forks separate.

I'll try coding your example when I get a chance. It should be in as a test anyway.