gpt4 book ai didi

javascript - 你今天如何在前端使用 es6 promises?

转载 作者:数据小太阳 更新时间:2023-10-29 04:19:08 26 4
gpt4 key购买 nike

我正在尝试使用 babel 来编译包含 es6 promises 的文件。我已经安装了 babel-cli、babel-preset-es2015、babel-plugin-es6-promise。

我的 .babelrc 配置是:

{
"presets": ["es2015"],
"plugins": ["es6-promise"]
}

我得到了编译后的 js 文件,里面有 require(),但我根本不想使用 require。

今天是否有可能在前端使用 es6 promises 而无需 js?

请提供任何指向 es6 promises 实现示例的 babel 链接(或者甚至 babel + require 因为我无法让 require js 正常工作)

ps:不想用webpack。

最佳答案

Is there any possibility of using es6 promises today on frontend side without require js?

在最新的 Chrome/Firefox/Safari/Edge 中,Promise 是 already supported natively .

如果您也在寻找旧版浏览器支持,这是一种无需 Babel 的简单方法就是只使用一个 polyfill 库。

以下是具有规范指定的确切行为的库:

这里有一些具有额外的自定义行为:

当然,以上只是一些比较流行的,但还有很多其他的你可以通过研究找到。

如果你确实想使用其他 ES6 功能,尤其是在旧版浏览器上,你可以继续添加 polyfill,但到那时,只使用 Babel 及其 env preset 可能更容易,也更面向 future 。 .


Please provide any link to es6 promises implementation sample with babel (or even with babel + require because i can't get require js working as well)

babel website's setup基本上描述了如何在您能想到的任何环境中使用它。如果 webpack 对你的需求来说太重了,我推荐 using gulp这是一个thorough guide on how

关于javascript - 你今天如何在前端使用 es6 promises?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39165273/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com