Answer the question
In order to leave comments, you need to log in
JavaScript how to run synchronous code asynchronously?
I want to read two files synchronously. But do it asynchronously. Files of different sizes, which is reflected in the names. This is necessary for understanding how asynchronous calls work. But I can't.
let fs = require('fs')
function read (file, callback) {
fs.readFileSync(file, 'utf8')
setTimeout(() => {
callback(file)
}, 0)
}
console.log('start')
read('big.txt', (result) => {
console.log('file', result)
})
read('small.txt', (result) => {
console.log('file', result)
})
console.log('end')
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question