Learning Javascript and Nodejs

Asked

Viewed 53 times

0

Gentlemen, I’d like your help in understanding the workings of Promises and async/await. I am new to the language and with a lot of cost I understood how callbacks work. Someone could help me translate the code below to Languages?

const mysql = require('mysql')
const connConfig = require('./dbConfig')
const connection = mysql.createConnection(connConfig.config.default)

const start = (query, arrayReturned) => {
    searchMenu(query, createArrayMenu,(options)=>{
        arrayReturned(options)
    })
}

const createArrayMenu = (row)=>{
    const options = []
    for (let index = 0; index < row.length; index++) {
        options[index] = [row[index].id_main_option,row[index].ds_main_option]
    }
    return options
}

const searchMenu = (query, getResults, transformResults)=>{
    connection.connect()
    connection.query(query,(err, results)=>{
        if(err) throw err
        transformResults(getResults(results))
    })
    connection.end()
}

module.exports = (query, test)=>{start(query, test)}
  • You can read this article (in Portuguese), which is an excellent introduction to the theme. :)

2 answers

1


When you want to make some kind of request or action that takes longer than normal (calling an API, for example), you need to configure whether this action will be necessary for future runs or not. That’s where the asynchronous and synchronous words come in.

Asynchronous: It is when you want the outcome of the action, but it is not necessary for the other scopes of the program. If you need to do something right after the action is finished, you can use Promises. Promises or "promises" is exactly what the name says, a promise that when the action ends, a code will be executed. The most common precedent is the .then. To create an asynchronous function in javascript, simply put async in front of the function, and when you want something asynchronous in the function, just use await before the action.

Synchronous: It is used to execute codes in sequence, being one dependent on the other

  • 1

    Thank you very much Murilo! The theory was very enlightening!!!

0

const mysql = require('mysql');
const { promisify } = require('util');
const connConfig = require('./dbConfig');
const connection = mysql.createConnection(connConfig.config.default);

const searchMenu = async (query)=>{
  connection.connect();
  const query = promisify(connection.query);
  const results = await query(query);
  connection.end();
  return results;
};

const createArrayMenu = (row)=>{
  const options = [];

  for (let index = 0; index < row.length; index++) {
    options[index] = [row[index].id_main_option,row[index].ds_main_option];
  }

  return options;
};

const start = async (query) => {
  const options = await searchMenu(query);
  createArrayMenu(options);
  return options;
};

module.exports = start;
  • thanks man! It was enlightening! Coming home I will train more on the theme!

  • @user1746040 do not forget to mark the answer as accepted if it was satisfactory

Browser other questions tagged

You are not signed in. Login or sign up in order to post.