Reputation: 334
I'm new to the Twilio APIs, and I can't seem to figure out how to count the tasks associated with a TaskRouter workspace.
const client = require('twilio')(accountSid, authToken);
client.taskrouter.workspaces.each(workspace => {
const allTasks = client.taskrouter.workspaces(workspace.sid).tasks;
var taskCount = 0;
allTasks.each(task => {
taskCount++;
});
})
That's easy enough...but I can't tell when the process is done. This each() function apparently doesn't return a Promise; allTasks.length is always 1 (because allTasks is a TaskListInstance, not an array); and the documentation isn't very detailed - it has basic examples, but I can't seem to find nuts-and-bolts API documentation that would tell me what I really need to know about each() or the TaskListInstance type.
Thanks in advance.
Upvotes: 0
Views: 175
Reputation: 334
Figured it out with a little of the old "sticktoitiveness." The each() function actually takes two parameters. The first is a TaskListInstanceEachOptions instance, which includes a "done" element that is a function that gets called when the list of tasks is exhausted. This can probably be written more elegantly - more Javascript-y - but here's what I finally found that works:
const allTasks = client.taskrouter.workspaces(workspace.sid).tasks;
var tasksEvaluated = 0;
var opts = {};
opts.done = function() { console.log('Done. Total # of tasks evaluated: ' + tasksEvaluated)};
allTasks.each(opts, task => {
tasksEvaluated++;
});
Upvotes: 1