webpack & async await
webpack & async await
ES 7
// async function f() {
// return 1;
// }
const f = async () => {
return 1;
}
f().then(value => console.log(value)); // 1
https://github.com/xgqfrms/WBP/issues/3
regeneratorRuntime
Uncaught ReferenceError:
regeneratorRuntime
is not defined
https://stackoverflow.com/questions/33527653/babel-6-regeneratorruntime-is-not-defined
https://github.com/babel/babel-loader/issues/161
https://templecoding.com/blog/2016/02/17/async-await-with-es6-babel-and-typescript/
regeneratorRuntime
https://babeljs.io/docs/usage/polyfill/
Babel includes a polyfill that includes a custom regenerator runtime and core-js.
https://babeljs.io/docs/plugins/transform-regenerator/
You need to use either the
Babel polyfill
or theregenerator runtime
so thatregeneratorRuntime
will be defined.
solution
https://babeljs.io/docs/usage/polyfill/
OR
https://babeljs.io/docs/plugins/transform-regenerator/
OK
.babelrc
{
"presets": ["env"]
}
webpack
require("babel-polyfill");
entry: ["babel-polyfill", "./src/backend-system/select-tree"],
Fetch API & Async Await
practical demo
const fetchJSON = (url = ``) => {
return fetch(url,
{
method: "GET",
// mode: "no-cors",
mode: "cors",
credentials: "same-origin",
headers: {
"Content-Type": "application/json; charset=utf-8",
},
})
.then(res => res.json())
.then(
(json) => {
return json;
}
)
.catch(err => console.log(`fetch error`, err));
};
// async / await
async function getDatas(url = ``) {
try {
return await fetchJSON(url);
} catch (err) {
console.error("getDatas error:\n", err);
}
}
async / await & Promise error handler
const fetchJSON = (url = ``) => {
return fetch(url,
{
method: "GET",
// mode: "no-cors",
mode: "cors",
credentials: "same-origin",
headers: {
"Content-Type": "application/json; charset=utf-8",
},
})
.then(res => res.json())
.then(
(json) => {
console.log(`json`, json);
return json;
}
)
.catch(err => console.log(`fetch error`, err));
};
// async / await
async function getDatas(url = ``) {
let result = ``;
try {
result = await fetchJSON(url);
} catch (err) {
console.error("getDatas error:\n", err);
}
console.log(`result`, result);
return result;
}
x = getDatas(`https://cdn.xgqfrms.xyz/json/fast/1.json`);
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/8964330.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2016-04-27 css 之 IE old version 兼容方法!