traverson
Version:
Hypermedia API/HATEOAS client for Node.js and the browser
37 lines (31 loc) • 1.08 kB
JavaScript
;
// TODO Only difference to lib/transform/fetch_resource is the continuation
// checking, which is missing here. Maybe we can delete this transform and use
// fetch_resource in its place everywhere?
var minilog = require('minilog')
, log = minilog('traverson')
, abortTraversal = require('../abort_traversal')
, httpRequests = require('../http_requests');
/*
* Execute the last step in a traversal that ends with an HTTP GET.
*/
// This is similar to lib/transforms/fetch_resource.js - refactoring potential?
function fetchLastResource(t, callback) {
// always check for aborted before doing an HTTP request
if (t.aborted) {
return abortTraversal.callCallbackOnAbort(t);
}
httpRequests.fetchResource(t, function(err, t) {
log.debug('fetchResource returned (fetchLastResource).');
if (err) {
if (!err.aborted) {
log.debug('error while executing http request');
log.error(err);
}
return t.callback(err);
}
callback(t);
});
}
fetchLastResource.isAsync = true;
module.exports = fetchLastResource;