简体   繁体   中英

Usage of Promise in webpack / react application

I am trying to use Promise in an react app using webpack but I have this error :

'Promise' is not defined no-undef

So far, everything was working well (used babel to translate js and jsx) but unfortunately Promise does not work.

Furthermore, I have this error in Chrome (latest) and I though Promise was buildin feature.... Am I right ?

Here is a piece of config that I use (I used survivejs kanban app as a starter, I am trying to add some functionalities). I did not change much the initial config :

From web pack.config.js

module: {
      loaders: [
        {
          test: /\.(js|jsx)$/,
          // Enable caching for extra performance
          loaders: ['babel?cacheDirectory'],
          include: include
        }
      ]
}

From .babelrc

{
  "presets": [
    "es2015",
    "react"
  ],
  "env": {
    "start": {
      "presets": [
        "react-hmre"
      ]
    }
  }
}

Failing code :

const locales = {
    en: () => require('react-intl?locale=en!./en.json'),
    fr: () => require('react-intl?locale=fr!./fr.json')
}

function loadLocaleData (locale) {
    return new Promise((resolve) => {
        locales[locale]()(resolve)
    })
}

THE VALID ANSWER IS IN COMMENTS

So far as I know you need te include a Promise polyfill in your webpack config. I use:

plugins: [
    new webpack.ProvidePlugin({
        'Promise': 'exports?global.Promise!es6-promise'
    })
],

Where es6-promise is a npm package which will be include when no promise is available or added by any other npm package

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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