Добавил еще ведущие нули к миллисекундам, если кому-то еще когда-то этот код понадобится
log = function (text, color, define){ var id, time, thread, logHtml, textLog; define = (typeof define == 'string') ? define.split(/[\s,.|:;]+/g) : define; if(typeof define === 'object' && define !== null){ if(Array.isArray(define)){ id = define.indexOf('id') > -1; time = define.indexOf('time') > -1; thread = define.indexOf('thread') > -1; } else{ id = define.id == true; time = define.time == true; thread = define.thread == true; } } else id = time = thread = true; id = id ? '<a href="action://action' + ScriptWorker.GetCurrentAction() + '" style="color:gray;">[' + ScriptWorker.GetCurrentAction() + ']</a>' : ''; time = time ? ' ' + getTime() : ''; thread = thread ? ' Поток №' + thread_number() : ''; logHtml = (id || time || thread) ? id + '<span style="color: white">' + time + thread + ' : </span>' : ''; logHtml += '<span style="color:' + (color ? color : 'white') + '">' + text + '</span>'; textLog = '[' + ScriptWorker.GetCurrentAction() + ']' + time + thread + ' : ' + text function getTime(){ var checkTime = function(i){ return (i < 10) ? "0" + i : i; }; var checkMilliSeconds = function(ms){ if (ms < 10) { return "00" + ms; } else if (ms < 100) { return "0" + ms; } else { return ms; } }; var d = new Date(); var hh = checkTime(d.getHours()); var mm = checkTime(d.getMinutes()); var ss = checkTime(d.getSeconds()); var ms = checkMilliSeconds(d.getMilliseconds()); return '[' + hh + ':' + mm + ':' + ss + '.' + ms + ']'; }; Logger.WriteHtml(logHtml, textLog); }Асинхронное скачивание файлов в node js
-
По форуму не нашел решения.
Имеем
- список ссылок (10 элементов)
- асинхронную функцию в Node js, для скачивания файлов
Promise.all - быстро завершает свою работу
Этот же код функции в VS-Code - отрабатывает нормально, файлы скачиваются параллельноКак правильно синхронизировать код, для асинхронного скачивания файлов ?
Поддерживается ли Promise.all встроенной nodejs ? -
@inotoxic said in Асинхронное скачивание файлов в node js:
По форуму не нашел решения.
Имеем
- список ссылок (10 элементов)
- асинхронную функцию в Node js, для скачивания файлов
Promise.all - быстро завершает свою работу
Этот же код функции в VS-Code - отрабатывает нормально, файлы скачиваются параллельноКак правильно синхронизировать код, для асинхронного скачивания файлов ?
Поддерживается ли Promise.all встроенной nodejs ?const fs = require('fs'); const axios = require('axios'); const urls = [ 'https://community.bablosoft.com/assets/uploads/files/1677659555720-1.txt', 'https://community.bablosoft.com/assets/uploads/files/1677659555723-2.txt', 'https://community.bablosoft.com/assets/uploads/files/1677659555724-3.txt' ]; const downloadPromises = urls.map(url => axios.get(url, { responseType: 'arraybuffer' })); await(new Promise((resolve, reject) => { Promise.all(downloadPromises) .then(responses => { responses.forEach(response => { const fileName = response.request.path.split('/').pop(); fs.writeFileSync(fileName, response.data); console.log(`${fileName} Скачен!`); }); console.log('Все файлы скачены'); resolve(); }) .catch(err => console.log(err)); })); -
@Fox said in Асинхронное скачивание файлов в node js:
Promise.all
Promise.all и так возвращает промис, можно просто await Promise.all ...