/*! @license ©2013 Ruben Verborgh - Multimedia Lab / iMinds / Ghent University */
/** Browser replacement for the request module using jQuery. */

var EventEmitter = require('events').EventEmitter,
    SingleIterator = require('../../lib/iterators/Iterator').SingleIterator;

require('setimmediate');

// Requests the given resource as an iterator
function createRequest(settings) {
  var request = new EventEmitter();

  // Delegate the request to jQuery's AJAX module
  var jqXHR = jQuery.ajax({
    url: settings.url,
    timeout: settings.timeout,
    type: settings.method,
    headers: { accept: settings.headers && settings.headers.accept || '*/*' },
  })
  // Emit the result as a readable response iterator
  .success(function () {
    var response = new SingleIterator(jqXHR.responseText || '');
    response.statusCode = jqXHR.status;
    response.headers = { 'content-type': jqXHR.getResponseHeader('content-type') };
    request.emit('response', response);
  });
  request.abort = function () { jqXHR.abort(); };

  return request;
}

module.exports = createRequest;

@@ -967,17 +967,182 @@
 e);%0A  %7D)
-;
+%0A  // Emit an error if the request fails%0A  .fail(function () %7B%0A    request.emit('error', new Error('Error requesting ' + settings.url));%0A  %7D);%0A  // Aborts the request
 %0A  reque

Add error handling to browser requests.