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

标签 javascript ecmascript-6 es6-promise

我正在尝试使用 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/

相关文章:

javascript - Ajax问题: delay in getting data from web service using innerHTML,请指导

javascript - 使用 Math.max 创建一个数组,使用传递给函数的最长长度的数组?

javascript - 有什么方法可以声明事件对象而不是在函数的参数中?

javascript - 为什么你不应该在 ES6 中使用 import all

node.js - 将 Sequelize 与 ES6 Promise 一起使用?

javascript - Promise.all 可以捕获错误的包装器

javascript - 对正则表达式的误解

javascript - 在回调中重用变量/对象

javascript - 当一个 promise 被拒绝时,使用 promise.all 对事务进行 Sequelize 无法回滚

javascript - 由于未定义的上下文而出现 TypeError