简体   繁体   中英

How to cancel pending API calls in Angular JS

I have a Search box and for every letter a user types I have 4 API calls being made. and if user types a long word , many API calls goes in pending state. with every new letter which user types how do i check and cancel if previous call is in pending state .

FController.prototype.typeAheadSearch = function (query) {
    var this_ = this;
    var dfd = this.$q.defer();
    this.$q.all([this.TypeAheadFactory.API.A(query),
                 this.TypeAheadFactory.API.B(query),
                 this.TypeAheadFactory.API.C(query),
                 this.TypeAheadFactory.API.D(query)])
        .then(function (responses) {

    // to do
            dfd.resolve(results);
        });

    return dfd.promise;
}

You can intercept the request and response and work on that. There is a concept of interceptors in angularjs. More can be found here: https://docs.angularjs.org/api/ng/service/ $http

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM