nilproject / NiL.JS

JavaScript engine for .NET written in C#.
BSD 3-Clause "New" or "Revised" License
333 stars 46 forks source link

Documentation: await all unsettled promises after Context.Run #202

Open viceice opened 4 years ago

viceice commented 4 years ago

How do I await all unsettled promises after Context.Run()?

js code

(async () => {
  await Promise.resolve();
  throw new Error('test');
})();

console.log('after');

cs code

var ctx = new Context();
ctx.Eval("<js code>");
// ctx.WaitForPromises() -> should throw on unhandled rejections
nilproject commented 4 years ago

There is currently no way to do this. Promise is a simple wrapper forTask. There is no object in which all created Promises are stored for a number of reasons. You can make your own solution for this.

viceice commented 2 years ago

Can the promises cause multi threading issues? eg when i create a list of them and await them all? Are they guaranteed to not run on seperate threads, like in nodejs, where we only have a single thread?

nilproject commented 2 years ago

It sounds not good, but no, we have no guarantees. Each promise will be evaluated in separate thread. This is because Promise in current implementation is just a wrapper for System.Threading.Tasks.Task. Yes, promises can cause some multi threading issues

viceice commented 2 years ago

And i can't override the Promise implementation, as it's pretty hard wired. 😕

nilproject commented 2 years ago

So, lets fix this. Please, look at #262. Is it possible to override Promise after these changes?