I have a script which calls Binance API. I receive a response for specific symbol like BTCUSDT. API doesn't allow to call multiple symbols in API endpoint.
This string - var j = schedule.scheduleJob('0 * * * *', function() is a node-schedule package which will call the script every hour at 00minutes(15:00/16:00/17:00...).
FULLCODE
var requestPromise = require('request-promise');
const { MongoClient } = require('mongodb');
const schedule = require('node-schedule');
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const fetch = require("node-fetch");
var today = new Date();
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date + ' ' + time;
var symbols = ["BTCUSDT", "ETHUSDT", "ATOMBTC"];
let cnt = 0;
const callIt = () => {
fetch(`https://api.binance.com/api/v3/klines?symbol=${symbols[cnt]}&interval=1h&limit=1`)
.then(res => res.json())
.then(data => {
const btcusdtdata = data.map(d => {
return {
Open: parseFloat(d[1]),
High: parseFloat(d[2]),
Low: parseFloat(d[3]),
Close: parseFloat(d[4]),
Volume: parseFloat(d[5])
}
});
console.log(btcusdtdata);
cnt++;
if (cnt < symbols.length) setTimeout(callIt, 3000)
})
.catch((err) => {
console.log(err);
})
};
const j = schedule.scheduleJob('0 * * * *', callIt)
Problem: This script call the properties inside an array one after another which is perfect for me. My problem is my node-schedule(0 * * * *)doesn't work.
I run the script and it's immediately send me the data. But I wan't to receive data only every hour and only after it call for properties in array. How I can insert a node-schedule function - const j = schedule.scheduleJob('0 * * * *', callIt) inside a main function.
Everything works well, only that the script suppose to receive data every hour not immediately when I start it.
rejectwithconsole.logconst j = schedule.scheduleJob('* * * * * *', callIt)and just havecallIt()instead ?