Created
April 11, 2024 03:33
-
-
Save RealYukiSan/edd24372527dc15528beea58589faf15 to your computer and use it in GitHub Desktop.
retry mechanism
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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