I plan to use async/await to run many tasks. Since I can't afford to do await on the tasks because main loop will keep spawning new tasks, I need to ensure the promise for the task is destroyed at the end. Does the following code snippet has a leakage of of promises? If there is a better way to achieve this, please let me know too.
const fetch = require ('node-fetch')
var gId = 0;
var ps = {}
var lId = gId; gId ++;
ps[lId] = task(lId)
lId = gId; gId ++;
ps[lId] = task(lId)
//will add many more tasks
async function task(id) {
console.log("starting..")
var url = "http://localhost/prod/te.php";
var r1 = await fetch(url).then((r) => r.text())
console.log(new Date() + "|" + id)
await wait(parseInt(r1) * 1000)
var r2 = await fetch(url).then((r) => r.text())
console.log(new Date + "|" + id)
ps[id].then(() => console.log("deleted promise " + id))
delete ps[id]
}
async function wait(ms) {
return new Promise (function (resolve, reject) {
setTimeout(function() {
console.log("expired")
resolve(0)
}, ms)
})
}