1

Traditionally, in synchronous languages a task such as having two nested foreach loops conditionally exit would be an easy task such as:

return new Promise(resolve, reject){
  AnArray.forEach(anElement){
      ACONSTANTARRAY.forEach(ACONSTANTELEMENT){
          if(anElement === ACONSTANTELEMENT){
              resolve(bar);
          }
      }
  }
  resolve(foo);
}

However, because foo will be resolved immediately what is the best way to handle this situation? Will I have to transform this into multiple Promise.all(array.map(function(){}) calls? Seems ugly, overly complex, and hard to understand.

hownowbrowncow
  • 465
  • 1
  • 5
  • 18
  • Where's your async code? We could help you better if you showed your async code. And, if there isn't async code, then there's likely no need for promises at all. Real code rather than pseudo code will gives us a better chance of offering the most useful answer. – jfriend00 Mar 03 '16 at 16:59
  • @jfriend00 I would like to keep things as asynch as possible. – hownowbrowncow Mar 03 '16 at 17:00
  • 1
    There no generic point in making a synchronous operation async. It ONLY makes your code more complicated than need be. `.forEach()` loops are synchronous. Always have been. Please show us real code so we can help you find a real solution to a real problem - not a theoretical solution to a problem that isn't described. What real problem are you trying to solve? – jfriend00 Mar 03 '16 at 17:00
  • @jfriend00 won't this create blocking issues? – hownowbrowncow Mar 03 '16 at 17:07
  • 1
    Your `.forEach()` loops are going to block as long as you're using `.forEach()`. They are synchronous, whether you're using promises or not. Promises do not make synchronous code magically become async. Promises are just a tool for coordinating operations that are already async. If you want to take a long synchronous operation and break it up so other things can be interleaved with it, then you have to either move it to another process or you have to break it up into chunks and run one small chunk at a time, with an async break between chunks that gives other things a chance to run. – jfriend00 Mar 03 '16 at 17:12
  • @jfriend00 so to remove blocking potentially and still achieve foreach functionality I should use something like Explosion Pills wrote below? – hownowbrowncow Mar 03 '16 at 17:14
  • Explosion Pill's answer does not help either. `AnArray.map()` is blocking. You need an actual asynchronous operation if you're going to have non-blocking code. Using promises does not make anything asynchronous. It only helps you coordinate things that are already asynchronous. – jfriend00 Mar 03 '16 at 17:16
  • If you want to see options for iterating over an array in chunks so it's non-blocking, see the various options here: http://stackoverflow.com/questions/10344498/best-way-to-iterate-over-an-array-without-blocking-the-ui/10344560#10344560. You can't use `.forEach()` or `.map()` or any built-in array iterator if you want to go async in chunks. Those iterators are synchronous (e.g. blocking) by design. – jfriend00 Mar 03 '16 at 17:17
  • If you want actual help here, you need to describe what real problem you're trying to solve. Your question as it currently stands is based on a false assumption that using promises with synchronous code is somehow "good". But, that's a false assumption and does not describe an actual problem you're trying to solve. – jfriend00 Mar 03 '16 at 17:18

1 Answers1

1

Sorry you don't like it, but if you need to wait for all of the asynchronous operations to complete before moving on you have to use Promise.all.

return Promise.all(AnArray.map(anElement =>
   new Promise((resolve, reject) => {
      if (ACONSTANTARRAY.find(ACONSTANTELEMENT => ACONSTANTELEMENT === anElement)) {
        return resolve(bar);
      }
      reject(foo);
   });
);

The above doesn't do anything asynchronous though. Keep in mind that there are many asynchronous libraries that create promises for you so you may not even need to do new Promise at all. You would still need to use Promise.all, though if you need to wait for multiple promises to resolve.

You could also consider using something like co for generator-based control flow that handles arrays of promises for you.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • 2
    It's unclear to me what actual problem this solves since there's no async code here. This seems like one of those cases where it's better to insist the OP explain an actual problem rather than answer a hypothetical that may be the wrong way to be going. – jfriend00 Mar 03 '16 at 17:03
  • @jfriend00 you're absolutely right; I even indicate this in my answer. I imagine that the actual array comparison he wants to do is asynchronous in some way but that definitely may not be the case. – Explosion Pills Mar 03 '16 at 17:25