Не могу разобраться с асинхронностью

Рейтинг: 0Ответов: 1Опубликовано: 30.01.2023

Делаю тг бота:

    const mysql = require("mysql2");
require('dotenv').config()
const TelegramApi = require('node-telegram-bot-api')
const bot = new TelegramApi(process.env.BOT_TOKEN, {polling: true})
var mysqlConfig = require('./my_sql_connect.js');
const cron = require("node-cron");
const shellexe = require('./requests/func.js')
const propotkl=5;
bot.setMyCommands([
  {command: '/add', description: 'Add node: /add xxx.xxx.xxx.xxx:9000'},
  {command: '/delete', description: 'Delete node: /delete xxx.xxx.xxx.xxx:9000'}, 
  {command: '/notif', description: 'show logs'},
  {command: '/notifoff', description: 'hide logs'},  
  {command: '/my', description: 'your nodes'}
])

 const start = () => {
    bot.on('message', async msg => {
      const text = msg.text;     
      
      //console.log(msg)
      if(text === '/start'){
        return bot.sendMessage(msg.from.id, Welcome!)
      }

      if(text === '/my'){
        console.log("нажат my")   
        bot.sendMessage(msg.from.id,Ваши ip: skljfkdsl)     
        return
      }
      return bot.sendMessage(msg.from.id, Unknown command)      
    })
  }
  start()
  
  //var pool=mysql.createPool(mysqlConfig);
  cron.schedule('* */1 * * * *', async () => { 

    const p = new Promise(function(resolve, reject){
      let main =[]
      for(var i = 0; i < 7;i++){
        main.push(shellexe(curl --max-time 3 --location --silent --request POST https://fullnode.testnet.sui.io:443 --header 'Content-Type: application/json' --data-raw '{ "jsonrpc":"2.0", "method":"sui_getTotalTransactionNumber","id":1}' 2>&1))
      }
      resolve(main)     
    })
    p.then(data =>{
      console.log(data)
    })    
  })

Мне нужно, чтобы часть с кроном не задерживало команды с функции старт. Вторая часть с кроном парсит сервера, проверяет работу нод. Когда нажимаю команду /my в start() должны выйти заведенные айпи с базы. Но при нажатии на /my очень долго жду когда закончится for, только потом зарабатывает start. Как сделать, чтобы start() сразу отвечал, не дожидался выполнения всего цикла фор?

Ответы

▲ 0

Оказалось shellexe блокирует весь поток. Поменял на курл.