0

An async/await call doesn't work. I loaded core-js and regenerator-runtime to fix that but it still isn't working. I'm guessing it's a matter of the proper components. That's why I posted my package.json and webpack.config.js files below.

const getPuzzle = async (wordCount) => {
    const response = await fetch(`//puzzle.mead.io/puzzle?wordCount=${wordCount}`, {
        mode: 'no-cors'
    })
    if (response.status === 200) {
        const data = await response.json()
        return data.puzzle
    } else {
        throw new Error('Unable to get puzzle')
    }
}

the error

Uncaught runtime errors:
ERROR
Unable to get puzzle
    at _callee$ (http://localhost:8080/scripts/bundle.js:392:17)
    at tryCatch (http://localhost:8080/scripts/bundle.js:367:1062)
    at Generator.<anonymous> (http://localhost:8080/scripts/bundle.js:367:3008)
    at Generator.next (http://localhost:8080/scripts/bundle.js:367:1699)
    at asyncGeneratorStep (http://localhost:8080/scripts/bundle.js:368:70)
    at _next (http://localhost:8080/scripts/bundle.js:369:163)

package.json:

{
  "name": "boilerplate2",
  "version": "1.0.0",
  "description": "",
  "main": "input.js",
  "scripts": {
    "dev-server": "webpack serve --open --mode development",
    "build": "webpack --mode production",
    "start": "webpack serve --open --mode development"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@babel/cli": "^7.25.6",
    "@babel/preset-env": "^7.25.4",
    "babel-loader": "^9.2.1",
    "core-js": "^3.38.1",
    "live-server": "^1.2.0",
    "regenerator-runtime": "^0.14.1",
    "webpack": "^5.94.0",
    "webpack-cli": "^5.1.4",
    "webpack-dev-server": "^5.1.0"
  }
}

webpack.config.js:

const path = require('path')

module.exports = {
    entry: ['core-js/stable', 'regenerator-runtime/runtime', './src/index.js'],
    output: {
        path: path.resolve(__dirname, 'public/scripts'),
        filename: 'bundle.js'
    },
    module: {
        rules: [{
            test: /\.js$/,
            exclude: /node_modules/,
            use: {
                loader: 'babel-loader',
                options: {
                    presets: ['@babel/preset-env']
                }
            }
        }]
    },
    devServer: {
        static: {
            directory: path.resolve(__dirname, 'public')
        },
        port: 8080,
        devMiddleware: {
            publicPath: "//localhost:8080/scripts/"
        }
    },
    devtool: 'source-map'
}

Any help is appreciated!

3
  • 1
    you're using mode: "no-cors" then expecting to read the result - that's the issue - nothing to do with core-js/regenerator-runtime which would only be needed for very old browsers - read what mode:"no-cors" means, then get rid of it from your code Commented Sep 24, 2024 at 1:08
  • Thanks for suggestion. Still not working. I get: Access to fetch at 'puzzle.mead.io/puzzle?wordCount=2' from origin 'localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.--- GET puzzle.mead.io/puzzle?wordCount=2 net::ERR_FAILED--- Uncaught (in promise) TypeError: Failed to fetch--- Any idea what's behind this? Commented Sep 24, 2024 at 1:58
  • Yes, this is a CORS issue. I could've guessed you used mode: "no-cors" in an attempt to overcome CORS issues, but CORS is always controlled by the server. i.e., the site puzzle.mead.io does not allow cross origin requests from browsers - you'll need to proxy the request through a server you control Commented Sep 24, 2024 at 2:04

1 Answer 1

0

The problem was not with Webpack or the components I chose. The problem was with the JavaScript in the async request:

const getPuzzle = async (wordCount) => {
    const response = await fetch(`//puzzle.mead.io/puzzle?wordCount=${wordCount}`, {
        mode: 'no-cors'
    })

mode: 'no-cors' should not have been there. Also, "https:" should have been in front of //puzzle... in the URL call. Once these 2 issues were fixed, everything was fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.