How to store JavaScript functions in a queue and execute in that order?


There are multiple ways to store JavaScript functions in a queue and execute them in order. Here are some of the methods:

1. Using an array to store functions

We can use an array to store the functions in the order we want to execute them. We can then use a loop to iterate over the array and execute each function in order. Here's an example:

const functionQueue = [];

functionQueue.push(() => console.log("Function 1"));
functionQueue.push(() => console.log("Function 2"));
functionQueue.push(() => console.log("Function 3"));

for (const func of functionQueue) {
  func();
}

In this example, we create an empty array called functionQueue. We then push three functions into the array using the push method. Finally, we use a for...of loop to iterate over the array and execute each function in order.

2. Using a linked list to store functions

We can also use a linked list to store the functions in the order we want to execute them. Each node in the linked list would contain a reference to a function and a reference to the next node in the list. We can then traverse the linked list and execute each function in order. Here's an example:

class Node {
  constructor(func, next = null) {
    this.func = func;
    this.next = next;
  }
}

class FunctionQueue {
  constructor() {
    this.head = null;
    this.tail = null;
  }

  enqueue(func) {
    const node = new Node(func);

    if (!this.head) {
      this.head = node;
      this.tail = node;
    } else {
      this.tail.next = node;
      this.tail = node;
    }
  }

  dequeue() {
    if (!this.head) {
      return null;
    }

    const func = this.head.func;
    this.head = this.head.next;

    if (!this.head) {
      this.tail = null;
    }

    return func;
  }
}

const functionQueue = new FunctionQueue();

functionQueue.enqueue(() => console.log("Function 1"));
functionQueue.enqueue(() => console.log("Function 2"));
functionQueue.enqueue(() => console.log("Function 3"));

let func = functionQueue.dequeue();
while (func) {
  func();
  func = functionQueue.dequeue();
}

In this example, we define a Node class that represents a node in the linked list. Each node contains a reference to a function and a reference to the next node in the list. We also define a FunctionQueue class that represents the queue of functions. The enqueue method adds a new node to the end of the linked list, and the dequeue method removes the first node from the linked list and returns its function.

We create a new FunctionQueue object called functionQueue and add three functions to it using the enqueue method. We then use a while loop to dequeue each function from the queue and execute it in order.

3. Using a Promise chain to store functions

We can also use a Promise chain to store the functions in the order we want to execute them. Each function would return a Promise that resolves when the function is done executing. We can then chain the Promises together using the then method to ensure that each function is executed in order. Here's an example:

function function1() {
  return new Promise((resolve) => {
    console.log("Function 1");
    resolve();
  });
}

function function2() {
  return new Promise((resolve) => {
    console.log("Function 2");
    resolve();
  });
}

function function3() {
  return new Promise((resolve) => {
    console.log("Function 3");
    resolve();
  });
}

function1()
  .then(function2)
  .then(function3);

In this example, we define three functions (function1, function2, and function3) that each return a Promise that resolves when the function is done executing. We then chain the Promises together using the then method to ensure that each function is executed in order. When we run this code, we should see "Function 1", "Function 2", and "Function 3" printed to the console in order.



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.