This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)=>{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('await-fs'); | |
(async () = > { | |
try{ | |
let json = await fs.readFile('package.json','utf8') | |
console.log(json) | |
} catch(err) { | |
console.log(err) | |
} | |
})() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs.readFile('/folder/somefile', (err, data) => { | |
if (err) throw err; | |
console.log(data); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
})); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = []; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}); |