Re:How JavaScript works: Event loop and the rise of Async pr...
by HeyImAlex on Saturday October 14, 2017 @02:04PM
(#15473351)
It can just be a boolean variable called locked?
[ Parent
| on HN ]
Re:How JavaScript works: Event loop and the rise of Async pr...
by nitely on Saturday October 14, 2017 @03:03PM
(#15473651)
Almost. In threaded programming a thread will wait/block until the lock is released and then make use of the protected resource. In async world you would probably just check some flag and if it's "locked" then register a callback to some "unlocked" event to mimic this.
Re:How JavaScript works: Event loop and the rise of Async pr...
by anonacct37 on Saturday October 14, 2017 @08:40PM
(#15475165)
Lookup twisted deferred locks. https://twistedmatrix.com/documents/9.0.0/api/twisted.intern...
Re:How JavaScript works: Event loop and the rise of Async pr...
by nitely on Saturday October 14, 2017 @08:54PM
(#15475223)
Looks nice. How is this implemented?
Re:How JavaScript works: Event loop and the rise of Async pr...
by rdtsc on Saturday October 14, 2017 @11:02PM
(#15475663)
Not gp, but I used Twisted Matrix for a few years. It is usually implemented with a boolean flag and a list of client waiting to be called back when the lock is released.
So users of the lock would call acquire and get back a deferred (a promise in other language), but say in the most basic way, a callback is passed to it. When lock can be acquired, that callback is called (or deferred is fired).
When done with the resource, must call the release. Release then will trigger the lock to inspect the list of available waiting clients, pick one and fire their callback letting them know they can access the resource, and so on.
https://github.com/twisted/twisted/blob/trunk/src/twisted/in...
Re:How JavaScript works: Event loop and the rise of Async pr...
by amelius on Saturday October 14, 2017 @03:40PM
(#15473812)
"Promises" are a lot like locks.
Re:How JavaScript works: Event loop and the rise of Async pr...
by ChristianBundy on Saturday October 14, 2017 @06:38PM
(#15474708)
Yep.
let lock;
const log = (msg) => {
lock = new Promise(async (resolve) => {
await lock;
console.log(msg);
setTimeout(resolve, 1000);
});
};
'hello world'.split('').forEach(log);