0

I recently started working with node and despite having read a lot about it I still have difficulties working with asynchronism concepts. I have the following scenario:

const mysql = require("mysql");

exports.mainFunction = () => { 
    productsArray = ['product 1', 'product 2', 'n...']; // A simple product array
    createLogFile(); 
    proccessProducts(productsArray);
   
    /* HERE I NEED TO RETURN "OK" WHEN BOTH FUNCTIONS (createLogFile and proccessProducts) ARE FINISHED 
    BUT HOW DO YOU KNOW WHEN THEY WERE FINISHED? */
}

const createLogFile = (logFileName) => {
    fs.writeFileSync("mylog.json", "") // Here I create a simple log file for future use.
}

const proccessProducts(productsArray) = {
    for (let p = 0; p < productsArray.length; p++) { // Here I iterate over the products array
      const newProduct = formatProduct(productsArray[p]); // Here I make some formatting in the product title
      saveProductInDatabase(newProduct); // And lastly I save the product in the database
    }
}

const saveProductToDataBase = (product) => {
    var connection = mysql.createPool({
        host: config.MYSQL_HOST,
        user: config.MYSQL_USER,
        password: config.MYSQL_PASSWORD,
        database: config.MYSQL_DB,
    });
    const query = "INSERT INTO products(Title) VALUES (?)"
    connection.query(query, product, (err) => {
       if (err) console.log(err)
    });
}

const formatProduct = (product) => {
   var productTitleList = product.split(" ")
   return productTitleList[1]
}

My difficulty here is to know when everything has already been executed in order to return an "ok" status in the main function... I don't exactly know how to work with promises and async/await in this environment. Could someone give me a light?

Tom Lima
  • 1,027
  • 1
  • 8
  • 25

1 Answers1

1

I didn't explain why this is, how it work, because there is many resources that answer those questions.

var connection = mysql.createPool({
    host: config.MYSQL_HOST,
    user: config.MYSQL_USER,
    password: config.MYSQL_PASSWORD,
    database: config.MYSQL_DB,
});

// There is nothing to do asynchronous, becouse `writeFileSync` blocking operation.
function createLogFile(_logFileName) {
    fs.writeFileSync("mylog.json", "");
}

// In this example we used `callback`,
// You can think this is main function, 
exports.mainFunction = async () => {
    try {
        let productsArray = ['product 1', 'product 2', 'n...']; // A simple product array
        createLogFile();
        // async function always return `promise` 
        await proccessProducts(productsArray);
        // include your logic here, when every Products had proccessed.
    } catch (error) {
        console.log(error);
        // handle error.
    }
}

// In order to use await keyword, you need async function...
async function proccessProducts(productsArray) {
    //  Here I iterate over the products array
    for (const product of productsArray) {
        const newProduct = formatProduct(product); // Here I make some formatting in the product title
        // You need to use 
        await saveProductInDatabase(newProduct); // And lastly I save the product in the database   
    }
    // in this example, results is executed in parallel, 
    // its OK to use string, bsc javascript ignore string, as like comments
    `await Promise.all(
        productsArray.map(product => saveProductInDatabase(formatProduct(product)))
     );`
}

// return Promise object,
function saveProductInDatabase(product) {
    const query = "INSERT INTO products(Title) VALUES (?)";
    return new Promise((res, rej) => {
        connection.query(query, product, (err, data) => err ? rej(err) : res(data));
    });
}

You can get some idea, about async/await , callback, promises from:

How to yield value multiple times from function?

learn more about async/await

learn more about calback

Nur
  • 2,361
  • 2
  • 16
  • 34