code: function(
      defer, userDefinedCallback, onlyData,
      bodyParser,
  ) {
      userDefinedCallback = userDefinedCallback || function() {};
  
      return function(err, response, body) {
          if (!err) {
              try {
                  if (response.statusCode >= 400) {
                      err = parseErrorResponse(response, body);
                  } else if (bodyParser) {
                      body = bodyParser(body);
                  }
              } catch (e) {
                  err = new Error(`Error parsing server response: ${e}`);
              }
          }
  
          if (err) {
              defer.reject(err);
              userDefinedCallback(err);
          } else {
              const res = {
                  code: response.statusCode,
  
                  // XXX: why do we bother with this? it doesn't work for
                  // XMLHttpRequest, so clearly we don't use it.
                  headers: response.headers,
                  data: body,
              };
              defer.resolve(onlyData ? body : res);
              userDefinedCallback(null, onlyData ? body : res);
          }
      };
  }
docs: /*
  Returns a callback that can be invoked by an HTTP request on completion,
  that will either resolve or reject the given defer as well as invoke the
  given userDefinedCallback (if any).
  
  HTTP errors are transformed into javascript errors and the deferred is rejected.
  
  If bodyParser is given, it is used to transform the body of the successful
  responses before passing to the defer/callback.
  
  If onlyData is true, the defer/callback is invoked with the body of the
  response, otherwise the result object (with `code` and `data` fields)