konzo
konzo

Reputation: 2093

angular $timeout after a successfull function call

I am calling a Cordova plugin from angular where i must put a certain delay between my calls to give the native hardware time to initialize or perform the task.

The time should be given even when just after they have return a success response.

The problem is everything i sent at once no matter what i try

// initial code removed as it hurts @Fizzix eyes.

Now the is perfect but not the timing...

 simplePrint: function(text, startEnterLines, endEnterLines) {
                var deferred = $q.defer();
                printer.open().then(
                    $timeout(printer.init(), 500)
                ).then(
                    $timeout(printer.writeEnterLine(startEnterLines), 1000)
                ).then(
                    $timeout(printer.printText(text), 1200)
                ).then(
                    $timeout(printer.writeEnterLine(endEnterLines), 1300)
                ).then(
                    deferred.resolve($timeout(printer.closePrinter(), 3000))
                );
                return deferred.promise;
            }

Upvotes: 0

Views: 54

Answers (1)

Aviro
Aviro

Reputation: 2155

A simple example how to use promise chains would be,

$q.when(true).then(function(value) {
    // Do work, return promise
}).then(function(value) {        
    // Do work, return promise
}).then(function(value) {
    // Do work, return promise
}).then(function(value) {        
    // Do work, return promise
}, function(value) {        
    // Do work
});

You should read more about chaining promises without breaking the sequence.
. Please find some good examples.

Upvotes: 2

Related Questions