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. :)
– Luiz Felipe