Re:A brief look at async-await
by mywittyname on Monday December 09, 2019 @10:14AM
(#21743270)
I think most JS programmers do something along the lines of...
function check() {
return new Promise(async (resolve, reject) => {
for(let i = 0; i < 6; ++i) {
if(await doCheck()) {
resolve(true);
}
}
reject();
});
}
or... async function check() {
for(let i = 0; i < 6; ++i) {
const status = await doCheck();
if(status) {
return status;
}
}
throw new Exception('failed the 6 checks');
}
Though, the same thing could certainly be accomplished with pure promise chains. One could also use the setTimeout method in situations where elapsed time is more important than a fixed number of calls.
Surely a better comparison for the async function would be a chain() that manually recreates the loop using a parameter or local captured variable, like shown below. They're still complex but if anything show the complexity more clearly, especially if 6 is not hardcoded but could be passed as a parameter. I suppose having the unrolled version from the article plus one of these would be best of all, to show the problem from all angles.
I'm not a JavaScript programmer so there could be mistakes in the above code. I'd be interested to know if there is, especially whether it was really necessary to declare the checkAndIterate variable on a separate line to its assignment.Surely the "manual" function could be implemented with some sort of loop-like