Skip to main content
Question

Await / async with fetch call


Hello,

 

I’m new to using await / async and can’t seem to get it working. If I delete the “const response = await” part of the below it works as needed, in random order, but as soon as I implement any await code it appears to stop at that line. The code finishes and doesn’t return errors.

The console log shows it stopping just before.

Any insights would be most appreciated.

 

thanks,

Josh

 

async function someFunction() {
        console.log('someFunction',"triggered")
        for (let i = 0; i < subitemName.length; i++) {

                let columnValues = {
                    "text": tagData[i],
                    "text_1": subgroupData[i],
                    "text_2": stationData[i],
                };

                const columnValuesStr = JSON.stringify(columnValues).replace(/"/g, '\\"');

                columnValues
                const mutation = `mutation {
                  create_subitem (parent_item_id: ${itemId}, item_name: "${subitemName[i]}", column_values: "${columnValuesStr}") {
                    id
                  }
                }`;

                console.log('i',i)

            //issues below /////////////////////////////////
                const response = await fetch('https://api.monday.com/v2', {
                  method: 'post',
                  headers: {
                    'Content-Type': 'application/json',
                    'Authorization': 'APIKEY'
                  },
                  body: JSON.stringify({
                    query: mutation
                  })
                })
                .then(response => response.json())
                .then(data => {
                  callback(null, data);
                })
                .catch(error => callback(error));

        }

}

someFunction();

output = {id: 1, test: "test"};

 

Did this topic help you find an answer to your question?
This post has been closed for comments. Please create a new post if you need help or have a question about this topic.