GatesPlan
GatesPlan

Reputation: 497

Using `$.Deferred()`, but one function fired before every pre-arranged functions

I was helped by previouse answers, question of mine, that I learned using jQuery command $.Deffered(). It was very useful and exciting moment but while proceeding, I found that one of my function fired before everything. Here is my code.

            function clearMsg() {
                var defer = $.Deferred();
                console.log('clearMsg() called..');

                $('#status > span').hide(400);

                setTimeout(function() {
                    defer.resolve();
                }, 400);

                return defer;
            };

            function addMsg( type, sign, text ) {
                var defer = $.Deferred();
                console.log('addMsg() called..');

                $('#status').append('<span class="text-'+type+'"><span class="glyphicon glyphicon-'+sign+'"></span> '+text+'</span> ').children(':last-child').show(400);

                setTimeout(function() {
                    defer.resolve();
                }, 400);

                return defer;
            };

            function addMsg_total() {
                var defer = $.Deferred();
                console.log('addMsg_total() called..');

                addMsg( 'success', 'ok', 'Total '+opt.total.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")+' nodes were found.' );

                setTimeout(function() {
                    defer.resolve();
                }, 400);

                return defer;
            };

            function addMsg_pagenation() {
                var defer = $.Deferred();
                console.log('addMsg_pagenation() called..');

                addMsg( 'info', 'ok', opt.pageNow+' of '+opt.pageEnd+' pages.' );

                setTimeout(function() {
                    defer.resolve();
                }, 400);

                return defer;
            };

.. and following is what I do.

clearMsg()
    .then(addMsg_total)
    .then(addMsg_pagenation)
    .then(addMsg('warning', 'warning-sign', 'Requested node was out of range.'));

.. I expected that the last addMsg() function start after the end of all sequence but addMsg() just start at first.

Why is that? What do I need to know to understand this?

Upvotes: 0

Views: 35

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388416

because you are invoking it by adding () at the end

clearMsg()
    .then(addMsg_total)
    .then(addMsg_pagenation)
    .then(function(){
        return addMsg('warning', 'warning-sign', 'Requested node was out of range.')
    });

Upvotes: 2

Related Questions