The checkIfFinished() method of a batch operation context can be used to mark a batch operation as "finished".

This is useful if you invoke asynchron operations like AJAX calls.

For the case that all operations have been marked as "finished", you can define a logic for that:

import Batch = require("nativescript-batch");
import Frame = require("ui/frame");
import HTTP = require("http");

var userListView = Frame.topmost().getViewById('myUserList');

var b = Batch.newBatch(function(ctx) {
    // make the ListView INVISIBLE
  
    userListView.items = ctx.items;
  
    ctx.checkIfFinished();
});

for (var userId = 1; userId <= 100; userId++) {
    b.items.push(userId);
  
    b = b.then(function(ctx : Batch.IBatchOperationContext) {
        var uid = ctx.items.getItem(ctx.index - 1).toString();
      
        HTTP.request({
            url: 'https://example.com/api/users/' + uid,
            method: 'GET'
        }).then(function(response) {
                    ctx.items
                       .push(JSON.parse(response.content));
          
                    ctx.checkIfFinished();
                },
                function(error) {
                    // handle error
          
                    ctx.checkIfFinished();
                });
    });
}

// this will be invoked when
// ALL operations have been marked
// as "finished"
b = b.whenAllFinished(function(ctx) {
    // make the ListView VISIBLE again
});

b.start();

Control invocation

By default the next operation is invoked automatic.

But you can control that by defining the InvokeStrategy for an operation:

import Batch = require("nativescript-batch");
import Frame = require("ui/frame");
import HTTP = require("http");

var userListView = Frame.topmost().getViewById('myUserList');

var b = Batch.newBatch(function(ctx) {
    // initialize async operations
  
    ctx.checkIfFinished();
});

for (var userId = 1; userId <= 100; userId++) {
    b.items.push(userId);
  
    b = b.then(function(ctx : Batch.IBatchOperationContext) {
        var uid = ctx.items.getItem(ctx.index - 1).toString();
      
        HTTP.request({
            url: 'https://example.com/api/users/' + uid,
            method: 'GET'
        }).then(function(response) {
                    // handle response
          
                    ctx.invokeNext();
                    ctx.checkIfFinished();
                },
                function(error) {
                    // handle error
          
                    ctx.invokeNext();
                    ctx.checkIfFinished();
                });
    }).setInvokeStrategy(Batch.InvokeStrategy.Manually);
       // now you have to call 'invokeNext()'
       // method in your operation context
       // to invoke the next operation
}

b = b.whenAllFinished(function(ctx) {
    // do 'completed' stuff here
});

b.start();

All operations that are marked as manually must call the invokeNext() method of an operation context to invoke the next operation.

You also can define the default stradegy by setting the value in the batch itself:

import Batch = require("nativescript-batch");
import Frame = require("ui/frame");
import HTTP = require("http");

var userListView = Frame.topmost().getViewById('myUserList');

var b = Batch.newBatch(function(ctx) {
    // initialize async operations
  
    ctx.invokeNext();
});

// set the default stradegy here
b.batch.invokeStrategy = Batch.InvokeStrategy.Manually;

// initialize your async operations...

b.start();