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?

6
  • dose saveProductInDatabase asynchronous? Commented Jun 2, 2021 at 18:33
  • I just updated the question with the full code :) @Nur Commented Jun 2, 2021 at 18:51
  • There is two answer, which one you prefer ? callback or promise or both? Commented Jun 2, 2021 at 18:52
  • The one that uses as little code as possible. Thank you very much!! :) Commented Jun 2, 2021 at 18:55
  • I didn't include any code, I just refactored you original code, those provide an examples that used, sync, callback, async/await, promise Commented Jun 2, 2021 at 19:33

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.