This document defines JavaScript Coding Crimes

${table_of_content}

A promise not returned takes a wrong turn

A promise used inside a chain of promise or a queue needs to be return to ensure synchronous code

Good Example:

function promiseA() {
  // Sleep / Delay for 1 second 
  return new RSVP.Queue()
    .push(function () {
      return RSVP.delay(1000)
    })
    .push(function () {
      console.log("promiseA is finished")
      return;
    });
} 

new RSVP.Queue()
  .push(function() {
    return promiseA()
  })
  .push(function() {
    console.log("Finished")
  })

>> promiseA is finishes
>> Finished

Bad Example:

function promiseA() {
  // Sleep / Delay for 1 second 
  return new RSVP.Queue()
    .push(function () {
      return RSVP.delay(1000)
    })
    .push(function () {
      console.log("promiseA is finished")
      return;
    });
}

new RSVP.Queue()
  .push(function() {
     promiseA()
   }
  .push(function() {
     console.log("Finished")
   })

>> Finished
>> promiseA is finished

Do not chain promises, Queue them: No "then", No "fail"

Chained promises do not support cancellation, unless specified. This is why queue should be used in order to avoid ghost Promises.

Good Example:

function promiseA() {
  // Sleep / Delay for 1 second
  return new RSVP.Queue()
    .push(function() {
      return RSVP.resolve()
    })
    .push(function () {
      console.log("I'm a ghost Promise");
    }, function (e) {
      console.log(e);
    });
}
promiseA().cancel()

Bad Example:

function promiseA() {
  // Sleep / Delay for 1 second
  return RSVP.resolve()
    .then(function () {
      console.log("I'm a ghost Promise");
    })
    .fail(function (e) {
      console.log(e);
    });
}
promiseA().cancel()

>> I'm a ghost Promise

${related_subject_list}

Bottom Gadget
Right Gadget