Skip to content

Instantly share code, notes, and snippets.

const mysql = require('mysql');
// standart example from mysql documentation
(function test1() {
const pool = mysql.createPool({
host: '127.0.0.1',
user: 'root',
password: '1234',
database: 'test',
});
const fs=require('fs');
const ab=['access','rename','ftruncate','chown','lchown','cmod','fchmod','stat','lstat','fstat','link','symlink',
'readlink','realpath','unlink','rmdir','readdir','close','open','utimes',
'futimes','fsync','write','read','readFile','writeFile','appendFile','mkdir','mkdtemp']
// fchown fdatasync mkdtemp rename truncate
ab.forEach(name=>{
if(!fs[name])return;
exports[name]=(...n)=>{
const fs = require('await-fs');
(async () = > {
try{
let json = await fs.readFile('package.json','utf8')
console.log(json)
} catch(err) {
console.log(err)
}
})()
fs.readFile('/folder/somefile', (err, data) => {
if (err) throw err;
console.log(data);
});
async function someFunction() {
const myArray = [1, 2, 3];
const connection = mysql.createPool({ options });
let resolvedFinalArray = await Promise.all(myArray.map(async(value) => { // map instead of forEach
const result = await asyncFunction(connection, value);
finalValue.asyncFunctionValue = result.asyncFunctionValue;
return finalValue; // important to return the value
}));
return functionThatUsesResolvedValues(resolvedFinalArray);
};
async function someFunction() {
const myArray = [1, 2, 3];
const connection = mysql.createPool({ options });
let finalArray = myArray.map(async(value) => { // map instead of forEach
const result = await asyncFunction(connection, value);
finalValue.asyncFunctionValue = result.asyncFunctionValue;
return finalValue; // important to return the value
});
const resolvedFinalArray = await Promise.all(finalArray); // resolving all promises
return functionThatUsesResolvedValues(resolvedFinalArray);
async function someFunction() {
const myArray = [1, 2, 3];
const connection = mysql.createPool({ options });
let finalArray = [];
myArray.forEach((value) => { // standard forEach
finalArray.push(asyncFunction(connection, value).then((result) => {
finalValue.asyncFunctionValue = result.asyncFunctionValue; // giving instructions
return finalValue; // important to return the value
}));
});
async function someFunction() {
const myArray = [1, 2, 3];
const connection = mysql.createConnection({ options });
let finalArray = [];
myArray.forEach((value) => { // standard forEach
finalArray.push(asyncFunction(connection, value)); // pushing promises into an array
});
const resolvedfinalArray = await Promise.all(finalArray); // resolving all promises
// iterate over some in order to do something like
// const iteratedResolvedfinalArray = [];
async function someFunction() {
const myArray = [1, 2, 3];
const connection = mysql.createConnection({ options }); // create db connection
let finalArray = [];
await asyncForEach(async (value) => {
const finalValue;
const asyncFunctionValue = await asyncFunction(connection, value); // async function that returns a promise
finalValue.asyncFunctionValue = asyncFunctionValue; // doing assignment operations with resolved value
finalArray.push(finalValue); // pushing each value to an array
});