Skip to content

Instantly share code, notes, and snippets.

@RealYukiSan
Created April 11, 2024 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RealYukiSan/edd24372527dc15528beea58589faf15 to your computer and use it in GitHub Desktop.
Save RealYukiSan/edd24372527dc15528beea58589faf15 to your computer and use it in GitHub Desktop.
retry mechanism
const { get } = require("node:https")
require('dotenv').config();
function fetch(url) {
return new Promise((resolve, reject) => {
get(url, (res) => {
let buff = Buffer.alloc(0);
res.on("data", chunk => buff = Buffer.concat([buff, Buffer.from(chunk)]))
res.on("error", reject)
res.on("end", () => resolve(buff))
}).on("error", e => {
if (e.code != 'ETIMEDOUT') reject(e)
fetch(url).then(resolve).catch(reject)
})
})
}
setInterval(() => {
fetch(`${process.env.BASE_URL}/getUpdates`).then(buff => {
console.log(JSON.parse(buff.toString()));
}).catch(console.log)
}, 2000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment