0

I was using async: false in ajax. I want to have the equivalent of this command in fetch. Apparently using async helps. But when I use the word async in the webpack, it does not work. i use webbpack 3.8.1 and in webpack.config.js:

const path = require('path');
const webpack = require('webpack');
  
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const extractCSS = new ExtractTextPlugin('allstyles_for_store_details_naghashi.css'/*'allstyles_for_store_details.css'*//*'allstyles.css'*/);
 
module.exports = { 
    entry: {
         'main': './wwwroot/source/app_for_store_details_naghashi.js' ,
     },
       
    output: {
        path: path.resolve(__dirname, 'wwwroot/dist'),
        filename: 'bundle_for_store_details_naghashi.js' ,
        publicPath: 'dist/', 
    },
    plugins: [
        extractCSS,
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery',
            Popper: ['popper.js', 'default']
        }),
        new webpack.optimize.UglifyJsPlugin(), 
    ],
    module: {
        rules: [
            {
                test: /\.css$/, use: extractCSS.extract(['css-loader?minimize'])
            }, 
            {
                test: /\.js?$/,
                use: {
                    loader: 'babel-loader', options: {
                        presets:
                            ['@babel/preset-react', '@babel/preset-env']
                    }
                }
            },           
        ]
    } 
};

in my file :

async function f12() {
    alert('13579-1122');  
}
f12(); 

It does not work when I use the word async. (I want the program to wait for the fetch command to complete when the fetch command is running. I was using async: false in ajax)

reza
  • 1
  • 1
  • Why is that function `async`? Also, you _don't_ want to use `async` (`async: false`) but you also _do_ want to use it? Your question makes no sense. – Andy Feb 13 '22 at 11:36
  • I want the program to wait for the fetch command to complete when the fetch command is running. I was using async: false in ajax. Please help me how to do this in fetch. I'm sorry I did not ask my questions correctly. – reza Feb 13 '22 at 11:48

1 Answers1

0

Have a function that just fetches the data, and call that function from an async function that awaits the response (JSON, presumably), parses it, and then you can do what you want with it.

A bit like this:

function getData(url) {
  return fetch(url);
}

async function main() {
  try {
    const response = await getData('https://example.com');
    const data = await response.json();
    console.log(data);
  } catch(err) {
    console.log(err);
  }
}

main();

Additional documentation

Andy
  • 61,948
  • 13
  • 68
  • 95
  • I want to know how to use it in webpack (in the file app_for_store_details_naghashi.js which I specified in the entry section inside webpack.config.js, it will not work if I use async) – reza Feb 13 '22 at 13:24
  • All modern browsers understand async/await. This doesn't sound like a webpack question. It's very difficult to understand what you're trying to do. – Andy Feb 13 '22 at 13:56
  • I searched the internet and came across posts like the following, but I could not fix my project problem: https://stackoverflow.com/questions/46389267/using-async-await-with-webpack-simple-configuration-throwing-error-regeneratorr – reza Feb 13 '22 at 14:34