function awaitForEventResponse (type, event, that, callback) {
  let response;

  if (!that.listenersResponses[event]) {
    // There is no response available yet for the targeted listener
    setTimeout(function() {
      awaitForEventResponse (type, event, that, callback);
    }, 10);
  } else {
    // oh yeah the listener got a message !
    response = that.listenersResponses[event];

    switch (type) {
      case 'create':
        if (!that.lastCreatedDocument) {
          // the previous action have not yet received a response from the server
          setTimeout(function() {
            awaitForEventResponse (type, event, that, callback);
          }, 10);
        } else {
          should(response._id).eqls(that.lastCreatedDocument._id);
          should(response.body).containEql(that.lastCreatedDocument.body);
          callback();
        }
        break;
      case 'update':
        if (!that.lastCreatedDocument) {
          // the previous action have not yet received a response from the server
          setTimeout(function() {
            awaitForEventResponse (type, event, that, callback);
          }, 10);
        } else {
          delete that.lastUpdatedDocument.lastModifiedAt;

          should(response._id).eqls(that.lastUpdatedDocument._id);
          should(response.body).containEql(that.lastUpdatedDocument);
          callback();
        }
        break;
      case 'delete':
        if (!that.response) {
          // the previous action have not yet received a response from the server
          setTimeout(function() {
            awaitForEventResponse (type, event, that, callback);
          }, 10);
        } else {
          should(that.response.status).eqls('ok');
          callback();
        }
        break;
      default:
        callback(new Error('Wrong event type (available types are create, update and delete'));
    }
  }
}
/**
 * Await for an event to be fired by the server and check its payload (message or data)
 * Also await for the last action to be performed (create, update or delete)
 *
 * @param type - the event type (create, update or delete)
 * @param event - the event root to listen too
 * @param that -  this !
 * @param callback -  the cucumber callback
 */
Await for an event to be fired by the server and check its payload (message or data)
Also await for the last action to be performed (create, update or delete)

@param type - the event type (create, update or delete)
@param event - the event root to listen too
@param that -  this .
@param callback -  the cucumber callback
Await for an event to be fired by the server and check its payload ( message or data ) Also await for the last action to be performed ( create update or delete ) @param type - the event type ( create update or delete ) @param event - the event root to listen too @param that - this . @param callback - the cucumber callback
function awaitForEventResponse (type, event, that, callback) {
  let response;
  if (!that.listenersResponses[event]) {
    setTimeout(function() {
      awaitForEventResponse (type, event, that, callback);
    }, 10);
  } else {
    response = that.listenersResponses[event];
    switch (type) {
      case 'create':
        if (!that.lastCreatedDocument) {
          setTimeout(function() {
            awaitForEventResponse (type, event, that, callback);
          }, 10);
        } else {
          should(response._id).eqls(that.lastCreatedDocument._id);
          should(response.body).containEql(that.lastCreatedDocument.body);
          callback();
        }
        break;
      case 'update':
        if (!that.lastCreatedDocument) {
          setTimeout(function() {
            awaitForEventResponse (type, event, that, callback);
          }, 10);
        } else {
          delete that.lastUpdatedDocument.lastModifiedAt;
          should(response._id).eqls(that.lastUpdatedDocument._id);
          should(response.body).containEql(that.lastUpdatedDocument);
          callback();
        }
        break;
      case 'delete':
        if (!that.response) {
          setTimeout(function() {
            awaitForEventResponse (type, event, that, callback);
          }, 10);
        } else {
          should(that.response.status).eqls('ok');
          callback();
        }
        break;
      default:
        callback(new Error('Wrong event type (available types are create, update and delete'));
    }
  }
}
function awaitForEventResponse ( type , event , that , callback ) { let response ; if ( ! that . listenersResponses [ event ] ) { setTimeout ( function ( ) { awaitForEventResponse ( type , event , that , callback ) ; } , 10 ) ; } else { response = that . listenersResponses [ event ] ; switch ( type ) { case 'create' : if ( ! that . lastCreatedDocument ) { setTimeout ( function ( ) { awaitForEventResponse ( type , event , that , callback ) ; } , 10 ) ; } else { should ( response . _id ) . eqls ( that . lastCreatedDocument . _id ) ; should ( response . body ) . containEql ( that . lastCreatedDocument . body ) ; callback ( ) ; } break ; case 'update' : if ( ! that . lastCreatedDocument ) { setTimeout ( function ( ) { awaitForEventResponse ( type , event , that , callback ) ; } , 10 ) ; } else { delete that . lastUpdatedDocument . lastModifiedAt ; should ( response . _id ) . eqls ( that . lastUpdatedDocument . _id ) ; should ( response . body ) . containEql ( that . lastUpdatedDocument ) ; callback ( ) ; } break ; case 'delete' : if ( ! that . response ) { setTimeout ( function ( ) { awaitForEventResponse ( type , event , that , callback ) ; } , 10 ) ; } else { should ( that . response . status ) . eqls ( 'ok' ) ; callback ( ) ; } break ; default : callback ( new Error ( 'Wrong event type (available types are create, update and delete' ) ) ; } } }
Await for an event to be fired by the server and check its payload (message or data)
 Also await for the last action to be performed (create, update or delete)
Await for an event to be fired by the server and check its payload ( message or data ) Also await for the last action to be performed ( create update or delete )
// There is no response available yet for the targeted listener // oh yeah the listener got a message ! // the previous action have not yet received a response from the server // the previous action have not yet received a response from the server // the previous action have not yet received a response from the server