Answer the question
In order to leave comments, you need to log in
How to execute functions synchronously?
Hello, please help me deal with nodes. I wrote some Javascript code, it is executed from top to bottom, everything is super clear. In a node with asynchrony, there is some other world. I can’t fully understand and figure out how I can perform actions in the node gradually, in order
, let’s say I do
let data = []
const fs = require('fs/promises');
fs.readFile('./data.txt', 'utf8',
async (error, d) => {
if (error) throw error;
data = await JSON.parse(d)
Answer the question
In order to leave comments, you need to log in
What prevents you from doing this through the usual await?:
async function fnOne() {
return new Promise(res=>setTimeout(()=>{res(1000)}, 1000))
}
async function fnTwo() {
return new Promise(res=>setTimeout(()=>{res(2000)}, 1200))
}
async function fnThree() {
return new Promise(res=>setTimeout(()=>{res(3000)}, 1400))
}
const allFn = async ()=>{
try {
await fnOne()
console.log('Выполнилась 1-я ф-ция')
const rz = await fnTwo()
console.log('Выполнилась 2-я ф-ция, полученный результат = ', rz)
await fnThree()
console.log('Выполнилась 3-я ф-ция')
}catch (e) {
console.error(e)
}
}
allFn()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question