Difference between revisions of "Short Notes on JS"

From PaskvilWiki
Jump to: navigation, search
(Created page with "== Fetch Patterns == <pre>fetch("url", { options... }) .then((response) => { if (!response.ok) throw new Error('Network response was not ok'); return response...")
 
Line 13: Line 13:
 
     console.error('Failed to fetch:', error);
 
     console.error('Failed to fetch:', error);
 
   });</pre>
 
   });</pre>
 +
 +
== 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:
 +
<pre>new webpack.DefinePlugin({
 +
  'process.env': {
 +
    NODE_ENV: JSON.stringify(process.env.NODE_ENV),
 +
    STAGE: JSON.stringify(process.env.STAGE),
 +
    // ...
 +
  }
 +
})</pre>

Revision as of 12:50, 28 January 2020

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),
    // ...
  }
})