r/javascript • • Oct 11 '17

JavaScript's Promise is Memory Unsafe

https://alexn.org/blog/2017/10/11/javascript-promise-memory-unsafe.html
70 Upvotes

77 comments sorted by

View all comments

Show parent comments

5

u/1-800-BICYCLE Oct 11 '17

Aw shucks, I thought that would fix it. For the last part, though, what I meant was (sorry, I had just woken up lol):

function loop(n) {
  return signal(n).then(i => {
     if (i % 1000 == 0) console.log(i)
     setTimeout(loop, 0, n + 1)
  })
}

Or maybe even

function loop(n) {
  return signal(n).then(i => {
    if (i % 1000 == 0) console.log(i)
    loop(n + 1)
  })
}

You can still sequence and aggregate the results without explicitly chaining the Promise object. I believe that libraries like bluebird do have this same flapMap optimization you’ve created, though (but I’ve started to prefer native for less dependencies).

5

u/alexelcu Oct 11 '17

I have not tried Bluebird, maybe it does it because the problem and the solution are pretty obvious, which is why I was surprised to see that the standard Promise doesn't do it.

5

u/1-800-BICYCLE Oct 11 '17

https://github.com/nodejs/node/issues/6673

You can read about the rationale there — seems to be an issue with either the Promise/A+ or ES6 spec.

3

u/alexelcu Oct 11 '17

Oh, that's useful, thanks.