I'm written an Promise class to block while async transfers json are done. For every async transfer I call before "addwaiter", when the transfer is done I call "removewaiter".
The promise class recall recursively and check if the counter is zero. If not, recall it again with an delay and hope it's now zero, which release "Promise resolve". If the maximal timeout is down, call reject.
Instead of the delayed recalling I looking for an efficently way to resolve the promise if removewaiter is called and the counter is zero.
But if a defined timeout is reached, reject should be called.
That was my first JS code:
class WaitForPrint {
constructor () {
this.success = 0;
this.count = 20
}
addWaiter() {
this.success++;
}
removeWaiter() {
this.success--;
}
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async mywait() {
if (this.success == 0) {
console.log("all fine nothing to waiting for...");
this.resolves(true);
} else {
console.log("waiting for count down!");
if (this.count-- > 0) {
this.sleep(500).then(() => { console.log("next waiting delay!"); this.mywait() } );
} else {
this.rejects(true);
}
}
}
waitforprint() {
return new Promise((resolve, reject) => {
this.resolves = resolve;
this.rejects = reject;
this.mywait();
});
}
}
Why I have to use this code? The Class with the Promise was called by an third party code. Which is running on a Node.JS with Chromium. The website will be called in Node.JS and after load, it should be printed as PDF. If the website is loaded, the print will start immediately even some asynchronous jobs are not finished yet, so I miss some data on the website. But the third party give a break point with promise. So long the promise not resolved, the print was delayed.
While I'm writting the clarification, I saw the solution to my first question an modified the code as bellow:
class WaitForPrint {
constructor () {
this.success = 0;
this.resolved = true;
this.timeout = 10000;
this.addWaiter();
}
addWaiter() {
this.resolved = false;
this.success++;
}
removeWaiter() {
this.success--;
if(this.success == 0 && this.resolved == false) {
console.log("all fine nothing to waiting for...");
this.resolved = true;
// If I don't use setTimeout() instead calling this.resolve directly the generated pdf does not show the requested data, which I'm waiting for - but the methode was called behind the modification on the html code :-/ - why?
setTimeout(() => { this.resolves(true); }, 500)
} else {
console.log("waiting for another async!");
}
}
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
waitforprint() {
this.removeWaiter();
return new Promise((resolve, reject) => {
this.resolves = resolve;
this.rejects = reject;
console.log("waiting for count down!");
if (this.count-- > 0) {
this.sleep(this.timeout).then(() => { if(!this.resolved) { this.rejects(true); console.log("error - timeout!");} })
} else {
this.rejects(true);
console.log("error - count down!");
}
});
}
}
this.resolves()andthis.rejects()? I don't see those method definitions.the third party give a break point with promise. So long the promise not resolved...Why don't you just attach a listener to that Promise, instead of having a timer? When the Promise resolves, run the print. You don't need a timer for that....but maybe I misunderstood what you meant. Generally though the whole point of Promises is that you can just listen for when they resolve and then do your action, without needing to resort to complicated, fragile timeouts and countdowns. This code is almost certainly an example of the XY Problem.