title: Stop running processes after a Promise is rejected
body: <p>I'm using the following code which working OK, but the problem is that when I get an error, I want it to stops all the other promises. For example if <code>chi.getCommand(val1, val2)</code>, will send a reject and I got to the exception catch, I want to cancel the promises for <code>chss.exe</code> and <code>app.getStatus(12);</code> How can I achieve that?</p>
  
  <pre><code>  var start = Promise.all([
        chi.getCommand(val1, val2),
        chi.findAndUpdateCustomer()
      ]).spread(function (command, customer) {
          return chss.exe(runnableDoc, command, customer)
                   .delay(10)
                   .then(function (val) {
                     if (val) console.log(val);
                     return app.getStatus(12);
                   });
      }).catch(function (err) {
          // catch and handle errors and when it come to here I want it to stops all the chain above
      });
  </code></pre>
  
  <p>This is the code of get command in short:</p>
  
  <pre><code>function getCommand(method, cmd) {
    return new Promise(function (resolve, reject) {
      ...
      child.stderr.on('data', function (data) {
          console.log('stderr: here!' + data);
          reject(data);
      });
  }
  </code></pre>
  
  <p>The console log <strong>stderr: here!</strong> are printed so the resolve are called!</p>
  
  <p><strong>UPDATE1</strong></p>
  
  <p>The only thing which stops the getStatus is when I put the <code>process.exit(1)</code> But this kill all the process, I just want to stop all the chain of the function getCommand in case Im <strong>arriving to the catch block</strong>,</p>
  
  <blockquote>
    <ol>
    <li>is there a way?</li>
    <li>is it bug in blueBird ? I use "bluebird": "2.9.34"</li>
    </ol>
  </blockquote>
  
  <p>function getCommand(method, cmd) {
        return new Promise(function (resolve, reject) {</p>
  
  <pre><code>var spawn = require('child_process').spawn;
  var ls = spawn("cmdbug",["/c","npm install express --save"]);
  
  
      ls.on('error', function (err) {
          console.log(err);
          reject(err);
      });
  </code></pre>
  
  <p>the error which I got is </p>
  
  <blockquote>
    <p>{ [Error: spawn cmdr ENOENT]
      code: 'ENOENT',
      errno: 'ENOENT',
      syscall: 'spawn cmdbug',
      path: 'cmdr',
      spawnargs: [ '/g', 'npm install express --save' ] }
    { [Error: spawn cmdbug ENOENT]
      code: 'ENOENT',
      errno: 'ENOENT',
      syscall: 'spawn cmdbug',
      path: 'cmdr',
      spawnargs: [ '/g', 'npm install express --save' ] }
    Child process failed with code  -4058</p>
  </blockquote>
  
  <p>And still the process of getStatus is writing to the console.</p>
  
  <p>The code which I use and not for testing is:</p>
  
  <p>The <strong>getCommand</strong> is the function that throw the <strong>error!</strong></p>
  
  <pre><code>var start= function () {
      return new Promise.all([
          childP.getChildProcessCommand(val1, val2),
          childP.findAndUpdateCustomer()
      ]).spread(function (cmd, updated) {
              //Execute child process
              return Promise.all([
                  childP.getCommand('spawn', cmd),
                  app.getStatus(51000,10,1);
              ]).catch(function (err) {
                  // catch and handle errors
                  console.log("An error occur: " + err);
                  return;
              })
          }).catch(function (err) {
              // catch and handle errors
              console.log("An error occur: " + err);
              return;
          })
  }();
  </code></pre>
  
  <p><strong>The code for check status is:</strong></p>
  
  <pre><code>// Returns a promise that resolves when the port is open
  
  checkPortStatus: function(port, host){
    return new Promise((resolve, reject) =&gt; {
      portscanner.checkPortStatus(port, host, function(error, status) {
        if(error)
          reject(error);
        else if(status === 'open')
          resolve(status);
        else
          reject(new Error('Port is not open'));
      });
    });
  },
  
  // THE API function
  getStatus: function(port, retriesLeft) {
  
    const TIME_BETWEEN_CHECKS = 1000;
    const HOST = '127.0.0.1';
    const RETRIES = 20;
    retriesLeft = retriesLeft === void 0 ? RETRIES : retriesLeft;
  
    if(!port) throw new Error('Port is required');
    if(retriesLeft === 0) Promise.reject('Timed Out');
  
    return new Promise((resolve, reject) =&gt; {
  
      // If it rejects, we do added work.
      this.checkPortStatus(port, host).then(resolve, error =&gt; {
       console.log("Waiting for port " + port + " attempt: " + retry);
        setTimeout(() =&gt; {
  
          this.getStatus(port, retriesLeft - 1).then(resolve, reject);
  
        }, TIME_BETWEEN_CHECKS);
      });
    });
  }
  </code></pre>
  
  <blockquote>
    <p>And I see the error in the console and still see the console log of
    the following for 10 attempts. console.log("Waiting for port " + port
    + " attempt: " + retry);</p>
  </blockquote>
  
  <p><strong>UPDATE2</strong>
  When trying to change As @artur suggest in the second option I got error in the recoursive call the error is:</p>
  
  <blockquote>
    <p>TypeError: Cannot read property 'then' of undefined </p>
  </blockquote>
  
  <p>This is what I've tried:</p>
  
  <pre><code>getStatus: function(port, retriesLeft) {
  
    const TIME_BETWEEN_CHECKS = 1000;
    const HOST = '127.0.0.1';
    const RETRIES = 20;
    retriesLeft = retriesLeft === void 0 ? RETRIES : retriesLeft;
  
    if(!port) throw new Error('Port is required');
    if(retriesLeft === 0) Promise.reject('Timed Out');
  
    var promise = new Promise((resolve, reject) =&gt; {
  
      // If it rejects, we do added work.
      this.checkPortStatus(port, host).then(resolve, error =&gt; {
       console.log("Waiting for port " + port + " attempt: " + retry);
        setTimeout(() =&gt; {
          //The error in the following recursive call
          this.getStatus(port, retriesLeft - 1).then(resolve, reject);
  
        }, TIME_BETWEEN_CHECKS);
        }).catch(function (error) {
           return reject(error);
       });
          return {
              promise:promise,
      cancel: function() {
          console.log('cancelling');
          clearTimeout(token);
          }
  
         }
      });
    });
  }
  </code></pre>