Short Notes on JS

From PaskvilWiki
Revision as of 12:50, 28 January 2020 by Admin (Talk | contribs)

Jump to: navigation, search

Fetch Patterns

fetch("url", { options... })
  .then((response) => {
    if (!response.ok)
      throw new Error('Network response was not ok');
    return response.json();  // or response.blob(), etc.
  })
  .then((data) => {
    // do something with the data received
  })
  .catch((error) => {
    console.error('Failed to fetch:', error);
  });

Environment Variables in WebPack

WebPack does not have access to environment (duh), so you need to "bake" any relevant environment variables in the WebPack during build:

new webpack.DefinePlugin({
  'process.env': {
    NODE_ENV: JSON.stringify(process.env.NODE_ENV),
    STAGE: JSON.stringify(process.env.STAGE),
    // ...
  }
})