@Fox 2 вопроса, 1ый, как я понял надо идти через цикл for, допустим мне еще в этом цикле надо проверить на валид, и если я укажу в цикле взять 40 куков, и 10 из них не валид, то по факту он использует всего 30, а надо 40, как можно это исправить?
и 2ой вопрос, я вот заметил что я выбрал папку в которой 20 файлов, а в ресурсах указано что всего 10 элементов(или он не указывает в ресурсах все элементы?)
NodeJS \ JS чтение файлов.
-
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) }); }));