Skip to content

Latest commit

 

History

History
58 lines (48 loc) · 912 Bytes

File metadata and controls

58 lines (48 loc) · 912 Bytes

Async rendering

webpack.config.js

module.exports = {
  entry: [
    'regenerator-runtime/runtime', // Async support!
    './js/app.js',
  ],
  mode: 'production',
  output: {
    path: './dist',
    filename: '[name].js',
    libraryTarget: 'umd',
  },
  rules: [{
    test: /\.js$/,
    use: {
      loader: 'babel-loader',
    },
  }],
  plugins: [
    new HtmlWebpackPlugin({
      template: './templates/index.html',
    }),
    new Plugin({ main: '#root' }),
  ],
};

templates/index.html

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head></head>
  <body>
    <div id="root"></div>
  </body>
</html>

js/app.js

export default async() => {
  const response = await fetch('https://api.com');
  const data = await response.json();

  const list = data.map(datum => `<li>${datum}</li>`).join('');

  return `<ul>${list}</ul>`;
};