简体   繁体   English

今天如何在前端使用es6 Promise?

[英]How do you use es6 promises today on frontend?

I'm trying to use babel to compile file that contains es6 promises. 我正在尝试使用babel来编译包含es6 promises的文件。 I have installed babel-cli, babel-preset-es2015, babel-plugin-es6-promise. 我已经安装了babel-cli,babel-preset-es2015,babel-plugin-es6-promise。

My .babelrc config is: 我的.babelrc配置为:

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

I got compiled js file with require() inside, but i don't want to use require at all. 我在里面有require()的情况下编译了js文件,但是我根本不想使用require。

Is there any possibility of using es6 promises today on frontend side without require js? 今天有可能在不需要js的情况下在前端使用es6 promises吗?

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) 请提供任何通向Bas的es6 Promise实现示例的链接(甚至使用babel + require,因为我也无法正常运行要求js)

ps: Don't wan't to use webpack. ps:不要使用webpack。

Is there any possibility of using es6 promises today on frontend side without require js? 今天有可能在不需要js的情况下在前端使用es6 promises吗?

In the latest Chrome/Firefox/Safari/Edge, Promises are already supported natively . 在最新的Chrome / Firefox / Safari / Edge中, 本地已支持 Promises。

If you're looking for an older browser support as well, an easy way without Babel is to just use a polyfill library. 如果您也在寻找较旧的浏览器支持,那么没有Babel的一种简单方法是仅使用polyfill库。

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 could keep adding polyfills but at that point it might be easier and more future-oriented to just use Babel and its env preset . 如果您确实想使用其他ES6功能,尤其是在较旧的浏览器上,则可以继续添加polyfill,但此时仅使用Babel及其env预设可能更容易且面向未来。


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) 请提供任何通向Bas的es6 Promise实现示例的链接(甚至使用babel + require,因为我也无法正常运行要求js)

The babel website's setup describes how to use it in basically any environment you can think of. babel网站的设置描述了如何在您能想到的基本上任何环境中使用它。 If webpack is too heavy for your needs, I'd recommend using gulp and here's a thorough guide on how 如果webpack而无法满足您的需求,我建议您使用gulp ,这是有关如何

Babel itself is just a transpiler, it translates from ES6 to ES5, and nothing else. Babel本身只是一个编译器,它从ES6转换为ES5,仅此而已。 Since ES6 includes modules, which can be used for imports like: 由于ES6包含模块,可用于导入,例如:

import Awesome from './Awesome'

Babel will transpile that into require statements for you, but not care about the actual requiring. Babel会为您将其转换为require语句,但不关心实际需求。 Therefore you need any framework that implements the AMD style require, like Browserify or something like that. 因此,您需要实现AMD风格所需的任何框架,例如Br​​owserify或类似的东西。 Webpack will also handle that for, so if you use Webpack + Babel, all the required code will be available and there is nothing in your way to use ES6 modules (and Promises, too) via the new import statement. Webpack还将为此进行处理,因此,如果您使用Webpack + Babel,所有必需的代码将可用,并且您无法通过新的import语句使用ES6模块(也包括Promises)。

I am using a webpack.config.js like that: 我正在使用这样的webpack.config.js

module.exports = {
    entry: ['babel-polyfill', './js/main.js'],
    output: {
        path: './bin/js',
        filename: 'main.js',
    },

    devtool: 'source-map',

    module: {
        loaders: [{
            test: /\.js$/,
            exclude: /node_modules/,
            loader: 'babel-loader'
    }]
  }
}

and a .babelrc like that: 像这样的.babelrc

{ 
    "presets": [ "es2015", "stage-3" ],
    "plugins": [
        "external-helpers",
        "transform-async-to-generator",
        "transform-runtime"
    ]
}

Before I started to use Webpack, I used Gulp and browserify, to first compile and than to bundle, but the setup with webpack is much simpler, so I decided to use that… 在开始使用Webpack之前,我先使用Gulp和browserify进行编译,而不是进行捆绑,但是使用webpack进行安装要简单得多,所以我决定使用…

Webpack + babel - for front. Webpack + babel-前置。 node.js 5+ version/babel-register for node.js node.js 5+版本/ babel-register for node.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM