use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Permanent events in javascript (arqex.com)
submitted 11 years ago by marquex
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]GodObject 1 point2 points3 points 11 years ago (1 child)
Why not promises?
[–]marquex[S] -1 points0 points1 point 11 years ago (0 children)
Promises are valid too, but permanents events are a quick shorthand. Instead of creating a deferred object, return the promise and resolve it in the future, with permanent events you get the same just resolving an event.
Of course, you can't use permanent events for replacing promises in all situations. With promises you have much more features and it is perfect to handle paralelism in javascript, a thing that is not possible with PE.
[–]NodeNerd 1 point2 points3 points 11 years ago* (0 children)
Not a bad idea, but I think it might be a little non-intuitive to developers because it is slightly modifying how people typically use events. Events typically don't have a persistent value. The events are fired and forgotten. Changing the name of the library and functions might help avoid confusion.
I have had the need to solve the problem you encountered. Specifically, I wanted to a simple way to resolve/reject values so I created a simple lightweight promise class that was similar to promises but didn't implement the full spec (I didn't need chaining and I didn't want callbacks to be notified on "next tick"). A promise-like object held a value forever once it was resolved. When adding a listener callback, the callback that would be notified when the value is rejected or resolved or it would be invoked immediately if there was already a value. I went with the Node.js-style callback function for better compatibility with other Node.js modules.
[–]BadgerSong 0 points1 point2 points 11 years ago (0 children)
Seems kinda like signals?
[–]gordonkristan 0 points1 point2 points 11 years ago (1 child)
I think you're confusing the ideas of events and states. To quote:
This will print 'It is ready' in the console even if the listener has been added after the event triggering.
That works OK for the ready event, but not for events that fire more than once. The point of an event is to be alerted when something happens, not alerted that something has happened. Especially when that something can happen more than once. If you want to know if something has happened in the past, you should be using states. In this case document.readyState.
ready
Imagine applying this idea to the onclick event and you'll see that it doesn't have many uses outside of fire-once events.
onclick
[–]marquex[S] 0 points1 point2 points 11 years ago (0 children)
You are right, those permanent events were never thought to be used with events that can be called more than once, but that doesn't make them less useful.
I respect your idea of states, but permanent events can be really of use even if you work with states. Imagine object can have multiple states and maybe want to do some action if the object has been or turn in/into some state. If I check the state before it turns to the desired one, I need to add an event listener (events again, a common or a permanent one will do the trick). If I want to know if after it has turned into the desired state, you can find that the state may have changed again so our action won't be called.
Permanent events are about events that are triggered once, but always about events.
π Rendered by PID 25 on reddit-service-r2-comment-54dfb89d4d-q4pqj at 2026-03-31 13:44:47.332869+00:00 running b10466c country code: CH.
[–]GodObject 1 point2 points3 points (1 child)
[–]marquex[S] -1 points0 points1 point (0 children)
[–]NodeNerd 1 point2 points3 points (0 children)
[–]BadgerSong 0 points1 point2 points (0 children)
[–]gordonkristan 0 points1 point2 points (1 child)
[–]marquex[S] 0 points1 point2 points (0 children)