4
For example, the code below that creates a async Generator:
async function* iterate() {
yield 1;
yield 2;
yield 3;
return null;
}
Could be used with the syntax for await...of
:
for await (const iteration of iterate()) {
console.log(iteration); // 1, 2, 3
}
The code that generates the iterator is from a library that uses Typescript, so it can be easily transposed into other versions of Ecmascript. But I want to use this third-party code without needing a transpilation process, maintaining compatibility with older browsers, how to use this code with ES5 syntax?
I made a test transpiring both this function and the loop that uses it, but a complex code is used with the help of various functions, making it difficult to understand and maintain
Could you exemplify? Depending on the project, adding a build step is not worth it, it would add unnecessary complexity. It may also be that the code you are going to use is from a legacy project that doesn’t even have a possible way to add transpilation
– Costamilam
@Costamilam if adding a build step is an unnecessary complexity, so you’ll have to wonder what gain you have in keep up a function that returns generators and awaiters. But the real question is: why is that what you’re doing nay supports this version of javascript? is that if it is a browser, it is a very old browser. If it’s nodejs -- why don’t you just upgrade to a version that supports es6 ? any solution will be better than making the generators and awaiters by hand.
– MoshMage
You assume that other solutions are simple, but they are not always, even in updating Node, which is in an environment that theoretically you have control, can generate many problems. ES6 does not have compatibility for Opera mini and partial for IE (generators, for example, do not have support) even in the latest versions, although they are little used browsers, they are still used, and compatibility is important
– Costamilam
I am not starting from the principle that all are simple, I simply look at the problem from a point of view of work that gives me to keep it in the future: Making a build step is stupidly easy when compared to making and maintaining generators and yields by resorting to old-fashioned promises -- it’s crazy. is doable? sure! but I abhor the days when I had to do it. However, and I forgot that, you can always turn to polyfills that already exist which I normally use when this rare problem appears. I edited the answer to reflect this.
– MoshMage