1

I'm trying to make a delay inside of $httpBackend response, and handle it with $resource service like this:

$httpBackend.whenGET(/\/courses\/\?id=\d+$/).respond(function(method, url) {
    var regexp = /\d+$/;
    var position = url.search(regexp);
    var course = url.slice(position);

    var courseToRet = angular.toJson(getCourse(course));

    var defer = $q.defer();
    $timeout(function() {
        defer.resolve(courseToRet);
    }, 500);

    return [200, defer.promise, {}];

});

And my $resource looks like this:

var resource = $resource('/courses/', null, {
    'update': {method: 'PUT'}
});

function getCourseResource(courseId) {
    return resource.get({id: courseId}).$promise;
}

In my controller I'm trying to get the data like this:

Course.getCourseResource(self.courseId).then(function (result) {
    console.log(result);
    result.$promise.then(function(data) {
        console.log(data);
        data.$promise.then(function(data2) {
            console.log(data2)
        });
    });
})

But it seems like it doesn't work. I'm out of ideas. In training purposes I have to do this in that exact way: by using $q in $httpBackend to make delay and get data by $resource. Thank you!

Saif Bechan
  • 16,551
  • 23
  • 83
  • 125
Vladimir Mironov
  • 655
  • 1
  • 7
  • 23
  • I've faced that problem when I wanted to test that the loading spinner is visible when page waits for response but it seems that `$httpBackend` waits for promise to be resolved before proceeding. I've ended up with a method in middleware to set up the delay for response in such cases – maurycy Dec 17 '15 at 13:45
  • @maurycy can you tell us a little more for that "method in middle ware"?? – Ceylan Mumun Kocabaş Jun 01 '16 at 14:48

0 Answers0