@slon said in BAS ничего не выводит при запуске модуля Node.js с этим кодом:
@Ajshma этот код ведь должен работать, или в басе есть какое-то ограничение?
https://community.bablosoft.com/topic/4450/websocket-на-node-js/6
BAS настолько особенный, что без опознавания https://wiki.bablosoft.com/doku.php?id=node.js писать что либо вообще походу без ошибок не возможно. Однако и после изучения статьи с переводчиком, я так ни черта и не понял.
const fs = require('fs').promises;
const path = require('path');
const FILE_LIST = [[FILE_SEARCH_RESULT]]
async function readFiles(filePaths) {
const newList = [];
for (const filePath of filePaths) {
try {
const content = await fs.readFile(filePath, 'utf8');
newList.push(content);
} catch (error) {
console.error(`Ошибка при чтении файла ${filePath}:`, error);
}
}
[[NEW_LIST]] = NEW_LIST;
return newList;
}
readFiles(FILE_LIST).then(newList => {
console.log('Содержимое файлов:', newList);
}).catch(error => {
console.error('Ошибка при чтении файлов:', error);
});
Работоспособность кода не проверял.
const fs = require('fs').promises;
const path = require('path');
const fileList = [[FILE_SEARCH_RESULT]];
async function readFiles(filePaths) {
const newList = [];
for (const filePath of filePaths) {
try {
const content = await fs.readFile(filePath, 'utf8');
newList.push(content);
} catch (error) {
console.error(`Error reading file ${filePath}:`, error);
}
}
return newList; // Returning the newList
}
(async function () {
await (new Promise((resolve, reject) => {
readFiles(fileList).then(newList => {
console.log('File contents:', newList);
resolve(); // <==== HERE <=======
}).catch(error => {
console.error('Error reading files:', error);
reject(); // Correctly rejecting the promise
});
}));
})();
@sergerdn Переменная NEW_LIST пуста. Даже после того как я изменил код.
const fs = require('fs').promises;
const path = require('path');
const fileList = [[FILE_SEARCH_RESULT]];
async function readFiles(filePaths) {
const newList = [];
for (const filePath of filePaths) {
try {
const content = await fs.readFile(filePath, 'utf8');
newList.push(content);
} catch (error) {
console.error(`Error reading file ${filePath}:`, error);
}
}
[[NEW_LIST]] = newList;
return newList; // Returning the newList
}
(async function () {
await (new Promise((resolve, reject) => {
readFiles(fileList).then(newList => {
console.log('File contents:', newList);
resolve(); // <==== HERE <=======
}).catch(error => {
console.error('Error reading files:', error);
reject(); // Correctly rejecting the promise
});
}));
})();
const fs = require('fs').promises;
const path = require('path');
const FILE_LIST = [[FILE_SEARCH_RESULT]]
async function readFiles(filePaths) {
const newList = [];
for (const filePath of filePaths) {
try {
const content = await fs.readFile(filePath, 'utf8');
newList.push(content);
} catch (error) {
console.error(`Ошибка при чтении файла ${filePath}:`, error);
}
}
return newList;
}
await(new Promise((resolve, reject) => {
readFiles(FILE_LIST).then(newList => {
console.log('Содержимое файлов: ' + newList);
[[NEW_LIST]] = newList
resolve()
}).catch(error => {
console.error('Ошибка при чтении файлов:', error);
reject(error)
});
}));