• notice
  • Congratulations on the launch of the Sought Tech site

Promiseresolve() is called but not resolved until the loop it was called on terminates

I am writing an application in nodejs based on a game loop.Each iteration of the loop fires an event emitter and calls an update method, like this: p>

 updateLoop() {
    while (!this.windowShouldClose()) {
      this.onUpdate.emit();
      this.update();
    }
  }

It seems that event emitters can help write async functions on game objects that wait for frames between operations , like a coroutine.I wrote a utility function that will call back or resolve the promise on the next fire of the event emitter:

nextFrame() {
  return new Promise((resolve)=> {
    this.onUpdate.once(resolve); //event emitter will resolve promise on next emit()
  })
}

nextFrameCallback(callback) {
  this.onUpdate.once(callback);
}

//example use
async doThingsAsync() {
  //do something
  await this.nextFrame();
  //do something else on the next frame/loop iteration
}

While the callback-based function works as expected, the promised version of nextFrame() does not I expect to resolve promises when.await nextFrame()Only in my example The outer loop in updateLoop()exit Only after will be resolved .I attached console.log to the promise to learn more and found the console log and resolve() is indeed called inside the loop, but waits It will still wait for the loop to terminate completely.

nextFrameDebug() {
  return new Promise((resolve)=> {
    this.onUpdate.once(()=> {
      console.log('debug nextFrame'); //this prints during the expected loop iteration
      resolve(); //this does not actually resolve until updateLoop() terminates
    })
  })
}

Here is a JSFiddle that demonstrates the above functionality:https ://jsfiddle.net/8L4wub29/5/ It seems like I'm close to a functional solution, but I have some misunderstandings about promises or async functions.Does this have anything to do with calling an async function from inside a loop? How do I write nextFrame() so that the promise is resolved on the next iteration of the loop, not after the loop exits? I realize that for most features in the game a timeout in milliseconds is more useful, but in some cases, like waiting for a physics update, The game may just want to wait one frame as a utility function.The callback based version works fine, but if you need to use it multiple times then you need to nest it, which doesn't seem as clean as using await

uj5u.com enthusiastic netizens replied:

Promise guarantees their .then Callback willAlways is called asynchronously.This means that if you had some random commits, you would do something like this:

somePromise.then(()=> {
  console.log('inside');
});
console.log('outside');

You will always see "outer" and "inner" even if the promise happens to be already resolved ".Promises are designed this way so you can determine the order of execution, so you don't have to fix painful bugs involving two orders.When you use await instead of explicit .then's: awaitmust wait until commit resolutioncode after , and the call stack is passed back to the system code.

So when you switch from callback to promise, you can never synchronize calls in the middle of the loop The code for the callback goes to the code that has to wait for the callback to be executed.After PromiseupdateLoopcodeonly onceback It can be run only if it is transmitted or generated.Since the loop is never passed back or yielded until it ends, all promises are delayed until after the loop.

If you needupdateLoop runs synchronously, then you can't use promises for that.If instead updateLoop should be asynchronous (maybe set a timeout before running the next step), then promises can be used, but I need more details on what you're trying to do with an example.

uj5u.com enthusiastic netizens replied:

Answering my own question as I found something that provides the intended functionality.I just updated the updateLoop() call in the example to this.

 async updateLoop() {
    while (!this.windowShouldClose()) {
      await null;
      this.onUpdate.emit();
      this.update();
    }
  }

Can someone comment more clearly why this is now having the desired effect? Changing the function to async doesn't affect the timing of the promise resolution unless I add an await statement.My assumption is that including an await statement releases the event loop that eventually allows the promise to resolve.If you edit the JSfiddle above with this function, the numbers printed in the console will not match what the print statement "expects"-only the counter increase about.The execution order seems to be correct.

Tags

Technical otaku

Sought technology together

Related Topic

1 Comments

author

buy atorvastatin 10mg online & lt;a href="https://lipiws.top/"& gt;order atorvastatin 40mg generic& lt;/a& gt; purchase atorvastatin online

Jdtryb

2024-03-10

Leave a Reply

+