JavaScript Method Chaining or Angular $q

onmyway

I am relatively new to JavaScript, and would appreciate your patience.

I am trying to chain my method calls to run asynchronously, but am a bit stuck.

I have done a lot of searching and tried various methods, but I am missing something.

The idea is to call one method after the other, but only once the first method has resolved.

I am using AngularJs, and I am not sure whether to use $q and $defer, or simple method chaining, or something completely different...

I have seen the below method of chaining:

callFirst()
.then(function(firstResult){
   return callSecond();
})
.then(function(secondResult){
   return callThird();
})
.then(function(thirdResult){
   //Finally do something with promise, or even return this
});

And this example of using $q:

app.service("githubService", function ($http, $q) {

    var deferred = $q.defer();

    this.getAccount = function () {
        return $http.get('https://api.github.com/users/haroldrv')
            .then(function (response) {
                // promise is fulfilled
                deferred.resolve(response.data);
                // promise is returned
                return deferred.promise;
            }, function (response) {
                // the following line rejects the promise 
                deferred.reject(response);
                // promise is returned
                return deferred.promise;
            })
        ;
    };
});

Below is my main function, and which method would be best for my purposes, and how would I implement the best solution?

NOTE: At this stage in my controller, my data has already been returned from my API call, and I am simply using the data to populate graphs and data grids:

function formatDataAccordingToLocation(dataFromAPI) {

    $scope.dataModel = DataModelService.dataLoaded();

    dataFromAPI.then(function (data) {

        $scope.totalItems = data.total_tweet_count;

        if ($scope.volumeChartChanged) {

            $scope.volumeChartChange = false;

            configureVolumeChart(data);

        }

        else {
            setSummaryPageData(data);

            setTweetListPageData(data);

            configureVolumeChart(data);

            configureMostMentionedGraph(data);

            configureFollowerGrowthGraph(data);

            configureEngagementsGraph(data);

            configureHashtagsGraph(data);

            configureTweetsVsReTweetsGraph(data);

            configureWordCloudGraph(data);
        }
    })
}

I know I am asking a lot, and would be very grateful for your help.

Research and resources:

https://docs.angularjs.org/api/ng/service/$q

Chain promises with AngularJS

https://schier.co/blog/2013/11/14/method-chaining-in-javascript.html

onmyway

As per the great feedback and comments from Paulson Peter and suzo, the following would be the answer to my question:

As my main function (formatDataAccordingToLocation)is inside my returned successful $http call, there is no need for me to chain these method calls with promises, and in doing so, delaying my execution.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Chaining promises with $q in Angular

From Dev

Chaining promises in Javascript and Angular

From Dev

confusion about chaining $q promises with angular and typescript

From Dev

Method chaining using javascript not working?

From Dev

chaining async method calls - javascript

From Dev

Method chaining using javascript not working?

From Dev

Javascript method chaining working but convoluted

From Dev

How to sleep a method in javascript method chaining

From Dev

How to sleep a method in javascript method chaining

From Dev

chaining http post and q service in angular in serial fashion

From Dev

Simplify Angular Functions that uses $q.defer() to promise chaining

From Dev

Promise chaining with Q nodejs

From Dev

Q and promises chaining

From Dev

Q function chaining

From Dev

Method chaining with the same method

From Dev

How do I Apply chaining method on javascript document.createElement

From Dev

Javascript - Can indentation on method chaining cause issues with semicolons?

From Dev

Is it possible to start method chaining on a new line after a variable name in JavaScript?

From Dev

chaining promises with q.js

From Dev

Nodejs / Q : Chaining promises sequentially

From Dev

Nodejs / Q : Chaining promises sequentially

From Dev

Swift - Method chaining

From Dev

Method chaining in ruby

From Dev

Return type for method chaining

From Dev

Python class method chaining

From Dev

Method Chaining and Class Inheritance

From Dev

Method chaining with asyncio coroutines

From Dev

Method Chaining based on condition

From Dev

Separating chaining method call