await/async transpilation question
ShiMeiWo opened this issue · 2 comments
ShiMeiWo commented
I tried this plugin and got stuck.
I made this two files:
// rollup.config.js
import nodent from 'rollup-plugin-nodent';
export default {
entry: 'src/scripts/Main.js',
dest: 'public/index.js',
format: 'iife',
sourceMap: true,
plugins: [
nodent(),
],
}
// src/scripts/Main.js
async function waitable(param){
return new Promise(function(resolve) {
console.log("!");
setTimeout(resolve, param);
});
}
async function main() {
console.log('A');
await waitable(1000);
console.log('B');
}
main();
I transpiled them and received it:
// public/index.js
(function () {
'use strict';
function waitable(param) {
return (function ($return, $error) {
return $return(new Promise(function (resolve) {
console.log("!");
setTimeout(resolve, param);
}));
}).$asyncbind(this, true);
}
function main() {
return (function ($return, $error) {
console.log('A');
return waitable(1000).then((function ($await_1) {
console.log('B');
return $return();
}).$asyncbind(this, $error), $error);
}).$asyncbind(this, true);
}
main();
}());
// "Uncaught TypeError: (intermediate value)(intermediate value).$asyncbind is not a function"
- How do I generate
$asyncbind()
function? - How can
Promise
inwaitable()
get transpiled?
Thanks.
oligot commented
- There is this issue in nodent regarding the $asyncbind function, but what I generally do is pass some options to nodent so that it doesn't depend on this function:
import nodent from 'rollup-plugin-nodent';
export default {
entry: 'main.js',
dest: 'bundle.js',
format: 'iife',
plugins: [nodent(
promises: true,
noRuntime: true
)],
sourceMap: true
};
- Promise are not transpilable; you have to use a polyfill like for example es6-promise