![](/img/trans.png)
[英]Using async/await with babel - require(“babel-polyfill”) line not at the top in built file
[英]using async/await without babel-polyfill
我的代码如下:
@action async login(payload){
try {
this.loginLoading = true
const data = await request('/admin/login', {
method: 'post',
data: payload
})
this.logined = true
localStorage.setItem('token', data.token)
this.loginLoading = false
} catch (error) {
console.log(error)
message.error('login failed')
this.logined = false
this.loginLoading = false
}
babel-polyfill会将以上代码转换为:
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
this.loginLoading = true;
_context.next = 4;
return (0, _request2.default)('/admin/login', {
method: 'post',
data: payload
});
case 4:
data = _context.sent;
this.logined = true;
localStorage.setItem('token', data.token);
this.loginLoading = false;
_context.next = 16;
break;
case 10:
_context.prev = 10;
_context.t0 = _context['catch'](0);
console.log(_context.t0);
_message2.default.error('login failed');
this.logined = false;
this.loginLoading = false;
case 16:
case 'end':
return _context.stop();
}
}
转换后的代码将使用源映射重命名chrome devl-tool中难以读取和调试的变量,实际上最新的chrome支持async / await语法,所以我不想在开发中使用babel-polyfill。
但是如果删除babel-polyfill,会抛出reinratorRuntime regeneratorRuntime is not defined.
如果您已将浏览器定位设置为chrome> = 59,那么您只需使用本机生成器,并且永远不会使用polyfill。
有趣的是:
如果你只需要生成器polyfill - 这是async / await所需的 - 那么你可以使用facebook / regenerator ,无论如何它都被babel-polyfill使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.