-
Notifications
You must be signed in to change notification settings - Fork 387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Promise API feedback #371
Comments
A pointer to a Promise can be used as a hashable identifier, i.e. you can have |
🤦 I was absolutely certain that pointers aren't a valid identifier and even opened the specification at some point in researching this ... apperantly I didn't re-read it though 🤦 |
What is your opinion on HostEnqueuePromiseJob ? |
I'm a bit confused. HostEnqueuePromiseJob is defined as a hook, but it is supposed to do the actual queueing, i.e. it doesn't sound to me like serves the same purpose as PromiseRejectionTracker (which is a hook that may have custom implementation). I guess from ECMAScript point of view they are the same, but from goja point of view they are not, I can't make it a hook (like PromiseRejectionTracker) because it won't have the necessary access to the Runtime to do the actual queueing. What do you need if for? |
Sorry for the long delay - I started answering and went to look something up ... and well here we are :(. I don't have a particular use case really. I found while investigating and reading the spec implementing the event loop in k6. In our case we have stranger (than usual for event loops) requirement - we actually need to exit the event loop (and end the iteration) if nothing is happening in the background and only then. Otherwise code such as export default () => {
makeHTTPRequestAndReturnPromise().then((resp) => {/* do something */})
} will just make promises and loop. (Note: for this use case k6 just runs the If A thing that would've helped is to be notified when a promise was created and resolved/rejected. The second part more or less being what Additionally a particular case that we currently have no way of handling is var res, rej, iter = 0;
export default function() {
if (iter ==0){ // do this only the first time
new Promise((r, j)=> {
res = r;
rej = j;
}).then((s) => console.log(__ITER, s))
} else {
res(5);
}
iter++;
} In this particular example in one iteration of k6 a user will create a promise and then on the next iteration resolve it. If we are really strict this should not be possible. But in reality global state is already shared between iterations so this isn't really a problem for us. Additionally unlike the previous example this is unlikely code, hopefully 🤞 . And the only way for this to be "fixable" in k6 is for goja to have a way to signal any change to a Promise, including it's creation, which seems like a lot of hooks/triggers. So in retrospect I do think that p.s:
I would expect that if I use
Thanks again for all the work 🙇 |
Promise ... identification, equalness, hashability issue
I have been working on adding an event loop to k6 and a this definitely shows that HostPromiseRejectionHandler usability is ... a problem.
I tried to copy the deno behaviour of "if a rejection doesn't get handled until the next time there are no task in the microtask queue - abort". Which seems fine but requiers something like
(This codes ... in practice for now adds another promise instead of being directly putted on the event loop, which is as I want it to be added before the event loop but the problem I will discuss stays the same)
As you can see I have to some how remember that I have done something for a promise (add it to the queue of rejected ones that I should abort on) and then on a (potential) later call find that promise and abort the action. But as a promise is not hashable I in practice do linear search in a slice. This likely isn't that big of a problem as I expect this will be rare occurance and I will be aborting after this so ... 🤷.
I do wonder though if we instead we can have some workaround for this, the two things coming to mind:
HostPromiseRejectionHandler
per promise. This will make it possible to in this case just set it to something that unqueues it through w/e mechanism seems most useful for the developer:(code written in this, so some small msitakes might exist ;) , but I hope you get the idea).
HostEnqueuePromiseJob
After this change which landed in ES2020 there is now
HostEnqueuePromiseJob
while previously to that change (at least to me) it sounded like goja needs to implement the Promise queue this addition makes it seem otherwise. I am probably wrong as at least in test262 there are zero references to that so it seems like it's still expected tobe implemented in the engine 🤷 .I was just wondering what your opinion on implementing this is, at least for now I don't have a use for it, but it will still be nice to have and as I am working on an event loop (mostly it's integration) I am wondering if I need to get ready to queue promises on my own :).
And in conclusion - thank you a lot for all the work 🎉 . Hopefully after I finish this I can go back to getting import/export support in goja 🤞
The text was updated successfully, but these errors were encountered: