今天在前台使用es6的承诺如何? [英] How do you use es6 promises today on frontend?

查看:182
本文介绍了今天在前台使用es6的承诺如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用babel来编译包含es6承诺的文件。我已经安装了babel-cli,babel-preset-es2015,babel-plugin-es6-promise。



我的.babelrc配置是:

  {
预设:[es2015],
plugins:[es6-promise]
}

我已经用require()里面编译了js文件,但是我根本不想使用require。



是否有可能在前端使用es6承诺,而不需要js?



请提供任何链接到es6承诺实施示例与babel(甚至与babel +要求,因为我不能要求js工作)



ps:不要不要使用webpack。

解决方案


/ p>

在最新的Chrome / Firefox / Safari / Edge,Promises是已经本地支持



如果您正在寻找较旧的浏览器支持,一个简单的方法,没有Babel,需要或任何东西只是使用polyfill库。



这里是具有规范指定的具体行为的库:





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





当然,以上只是一些更受欢迎的,但还有很多其他可以通过研究找到。 p>

如果您想使用其他ES6功能,特别是旧版浏览器,您仍然需要babel。







请提供任何链接到es6承诺实施示例与babel(甚至与babel +要求,因为我不能要求js工作) / p>

babel网站的设置描述了如何在基本上可以考虑的环境中使用它。如果 webpack 对您的需求太重,我建议使用gulp ,这里有一个关于的详细指南


I'm trying to use babel to compile file that contains es6 promises. I have installed babel-cli, babel-preset-es2015, babel-plugin-es6-promise.

My .babelrc config is:

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

I got compiled js file with require() inside, but i don't want to use require at all.

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

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)

ps: Don't wan't to use webpack.

解决方案

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

In the latest Chrome/Firefox/Safari/Edge, Promises are already supported natively.

If you're looking for an older browser support as well, an easy way without Babel, require or anything is to just use a polyfill library.

Here are libraries that have the exact behavior specified by the spec:

And here are some with additional customized behavior:

Of course, the above are just some more popular ones but there are many others you can find by researching.

If you do want to use other ES6 features, and especially on older browsers, you would still need babel.


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)

The babel website's setup describes how to use it in basically any environment you can think of. If webpack is too heavy for your needs, I'd recommend using gulp and here's a thorough guide on how

这篇关于今天在前台使用es6的承诺如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆