Skip to content

Task

listr2 is a collection of tasks that are housed in a single instance as we have just created. Therefore the Task is the smallest building block of your task list.

Task

A single task is an object with the given properties, where the task is the main attraction that the desired function gets executed.

A task can be in the form of, which is ensured by the typings:

  • Function/Promise
  • Listr [1]
  • Stream
  • Observable

Creating Your First Task

ts
import { Listr } from 'listr2'

interface Ctx {
  /* some variables for internal use */
}

const tasks = new Listr<Ctx>(
  [
    {
      title: 'This task will execute.',
      task: async (ctx): Promise<void> => {
        // perform some operations
      }
    }
  ],
  {
    /* options */
  }
)

try {
  await tasks.run()
} catch (e) {
  console.error(e)
}

Append To Existing Listr

ts
import { Listr } from 'listr2'

interface Ctx {
  /* some variables for internal use */
}

const tasks = new Listr<Ctx>([], {
  /* options */
})

tasks.add([
  {
    title: 'This task will execute.',
    task: async (ctx): Promise<void> => {
      // perform some operations
    }
  }
])

try {
  await tasks.run()
} catch (e) {
  console.error(e)
}

  1. A subtask must be created through the helper function of task.newListr since there are injections of singleton instances of parent task performed while creating a subtask. Please check out the related section. ↩︎