This is my code:
let observe = (obj: any, fn: any) => new Proxy(obj, {
set: (obj, key, val) => {
fn(obj)
return obj[key] = val;
}
});
router.use('/', async (req, res) => {
const id = req.query.id
arrs = observe([], (arr: any) => {
console.log("current", id)
});
setTimeout(() => {
arrs.push(id);
}, 5000);
}
Everything is ok, but console.log("current", id)
does not work correct.
1- I open this url
http://localhost:9000/?id=a
2- after one second, I open this url:
http://localhost:9000/?id=b
3- after five seconds, id
push to arrs
and console.log("current", id)
printed:
current b
current b
why?!
The correct result most be:
current a
current b