Closed sormy closed 5 years ago
It works for me using the link below. I'm guessing some other babel transform is munging the code and stripping the .$asyncbind
which ensures this
is set correctly.
As a point of information, there is no point in returning a Promise
from an async function
, or indeed declaring it async
- you can just return a Promise
and await
on that
Output:
function loadBundle() {
return (function ($return, $error) {
return $return(new Promise((resolve, reject) => {
if (this.xhr) {
this.xhr.abort();
this.xhr = undefined;
}
this.xhr = $.ajax({
url: "some url",
dataType: "script",
success: () => {
resolve();
},
error: (jqXHR, textStatus, errorThrown) => {
reject();
}
});
}));
}).$asyncbind(this, 0);
}
Hi, still trying to figure out why $asyncbind
is not in output. However, having async
explicitly defined for function is very convenient since it helps static analyzers to warn if I forgot to add await
before function call.
Seems like fast-async
needs two plugins fromenv
preset to be disabled to work well. I guess it could be added to documentation for the plugin. There are no issues if these two transforms are disabled.
{
"presets": [
["@babel/env", {
"exclude": ["transform-regenerator", "transform-async-to-generator"]
}]
],
"plugins": [
"module:fast-async"
]
}
Thanks for the update. Babel is a moving target, and this repo is pretty much in maintenance only. If you want to do a PR for the README, I'd be happy to accept it.
This code:
converted to:
and
this
is always undefined here becausereturn new Promise(function ($return, $error) {
stealsthis
.package.json:
babel.config.js: