Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript

How to use AutoInject in JavaScript's async module

Hassan Azhar

AutoInject

The autoInject function allows for the asynchronous execution of a number of functions. The function decides the order of execution and the number of functions to execute in parallel based on the dependencies of each function.

Usage

The following example demonstrates how autoInject can be used.

import autoInject from 'async/autoInject';
// Sleep function
const delay = time => new Promise(res => setTimeout(res, time));

autoInject({
  first_function: async function(callback){
    console.log("first_function executing")
    await delay(1000);
    console.log("first_function executed");
    callback(null, "first");
  },
  second_function: async function(callback){
    console.log("second_function executing");
    await delay(5000);
    console.log("second_function executed");
    callback(null, "second");
  },
  third_function: function(first_function, callback){ // Dependent on first function
    console.log("third_function executing");
    console.log("third_function executed");
    callback(null, first_function + " third");
  },
  fourth_function: function(second_function, third_function, callback){// Dependent on third and second functions
    console.log("fourth_function executing");
    console.log("fourth_function executed");
    callback(null, third_function + " " + second_function + " fourth");
  },
}, function(err, result){
  if (err){
    // Displays error
    console.log(err);
  }
  else {
    // Displays order of execution
    console.log(result.fourth_function);
  }
})

autoInject({
  first_function: async function(callback){
    console.log("autoInject 2: first_function executing")
    console.log("autoInject 2: first_function executed");
    callback(null, "first");
  },
  second_function: async function(callback){
    console.log("autoInject 2: second_function executing");
    // Suppose that an error occurs here
    // Error message sent as first argument
    // All other functions are skipped
    // Final callback called
    callback("Second autoInject Error Message", null)
  },
  third_function: function(first_function, callback){
    console.log("autoInject 2: third_function executing");
    console.log("autoInject 2: third_function executed");
    callback(null, first_function + " third");
  },
  fourth_function: function(second_function, third_function, callback){
    console.log("autoInject 2: fourth_function executing");
    console.log("autoInject 2: fourth_function executed");
    callback(null, third_function + " " + second_function + " fourth");
  },
}, function(err, result){
  if (err){
    // Displays error
    console.log(err);
  }
  else {
    // Displays order of execution
    console.log(result.fourth_function);
  }
})

This example demonstrates both the successful and erroneous execution of the autoInject function. The function takes a list of functions as its first parameter and an optional callback in its second parameter. The callback takes any error as its first parameter and any value as its second parameter. If no callback is given, a promise is returned by the autoInject function.

Each function in the sequence has its dependencies and a callback in its parameters. The callback is defined automatically by the autoInject library. The callback is called with null as the first parameter. This tells the program that no error has occurred. If an error has occurred, the first parameter should be a non-null value. Any parameters that are to be sent to any functions dependent on the current function will come after the first parameter, i.e., the one that indicates the presence of an error.

The first autoInject sequence executes without error. As stated earlier, the dependencies for any functions are passed to those functions as arguments. These arguments also contain any data forwarded by the dependee functions. In this example, third_function is dependent on first_function, and fourth_function is dependent on third_function and second_function. This means that the third function cannot run before the first function has completed execution. Similarly, the fourth function cannot start execution before the second and third functions complete execution.

The second autoInject sequence assumes that an error occurs in the second function. When an error occurs, the callback should be called with the first argument as a non-null value. This skips all other functions in the autoInject sequence and calls the final callback with a non-null value.

RELATED TAGS

javascript

CONTRIBUTOR

Hassan Azhar
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring