Page 1 of 1

await/async syntax

Posted: 2017-07-11, 21:33
by cyisfor
There's a nifty idea in javascript lately that is a syntax for making Promises easy to use. Palemoon doesn't support it, but I'd certainly appreciate if it did. Basically something like
async function someops() {
let a = await getapromise();
let b = await getanotherpromise();
return a + b
}
would be transformed into something like:
function someops() {
var trace1, trace2, trace3;
trace1 = getastacktrace();
return getapromise().then(function (a) {
trace2 = getastacktrace();
return getanotherpromise().then(function(b) {
trace3 = getastacktrace();
return a + b;
}).catch(function(e) {
global_handle_error(e,combine_traces(trace3,trace2,trace1));
});
}).catch(function(e) {
global_handle_error(e,combine_traces(trace2,trace1));
});
}

...except written by someone who actually understands how to javascript.

async/await syntax allows for asynchronous error handling using try/catch blocks, eliminates any callback hell, preserves stack traces, is easier to debug, and um... allows for certain optimizations that passing a function closure to then() couldn't take advantage of if it wasn't built into the language.

Has there been any luck just... substituting a newer javascript engine in, that supports await syntax?

Re: await/async syntax

Posted: 2017-07-11, 23:25
by Moonchild
cyisfor wrote:Has there been any luck just... substituting a newer javascript engine in, that supports await syntax?
Haha if only it were that simple. No, despite SpiderMonkey being usable as a standalone interpreter, it's not possible to substitute the engine for a different version, because it is tightly interwoven with DOM code, and Mozilla has been completely rearchitecturing most of how JS talks to the rest of the browser, not to mention tons of renaming and moving things around.
Also, async/await is not part of ES6 (it's a draft proposal for ES2017+); you shouldn't be using it on sites unless you really don't care about being browser-agnostic or mobile/embedded friendly.
If you want this kind of functionality in browsers-of-today, then you can look into e.g. https://github.com/facebook/regenerator to convert your code into plain old JS; most if not all asynchronous functions in JS can be implemented using JS itself.

Re: await/async syntax

Posted: 2017-07-12, 09:26
by JustOff
Meanwhile, they run like crazy: ECMAScript 2017 was approved on June 27, 2017 :twisted:

Re: await/async syntax

Posted: 2017-07-12, 11:19
by Moonchild
JustOff wrote:Meanwhile, they run like crazy: ECMAScript 2017 was approved on June 27, 2017 :twisted:
Of course they are. It's all they do. "Let release a new "standard" every year" -- they are so disconnected from people actually implementing all of this.
just because a committee approves it, doesn't mean it'll be adopted any time soon, if at all.

Re: await/async syntax

Posted: 2017-07-17, 08:03
by cyisfor
Thanks for looking into it at least. I don't really care if it's standard or approved or whatever. I just like async/await because it catches a ton of common errors and has a cleaner syntax than promises, and the javascript language really shouldn't have no support at all for asynchronous stuff.

This is, I should add, a feature request, not a bug report. I'm free to submit patches if I expect it to happen. For now, I just want to mention that it'd be neat to have, and I couldn't find any other thread on the subject.

Re: await/async syntax

Posted: 2017-07-27, 03:40
by xtal256
It's been a while since I did anything in JavaScript, but I used to use babelify with the async plugin to allow me to write async/await code which would be transformed at build/compile time into compatible JavaScript code.
Though looking back at it now, the Facebook one Moonchild linked to is probably easier to use.