2

So I am playing around with node.js and trying to get push some data to an object from a function. Everything is being parsed correctly with the regex, I guess I just don't understand how to implement what I am doing correctly. I am getting undefined on the json objects after I parse the game data.

const diplo = `!report Game Type: Diplo
1: <@12321321421412421>
2: <@23423052352342334>
3: <@45346346345343453>
4: <@23423423423523523>`


var diplo_data = {players:[]};

async function gameType(game) {
    let data = {};
    let game_types = [{id: 1, name: 'Diplo'}, {id: 2, name: 'Always War'}, {id: 3, name: 'FFA'}
        , {id: 4, name: 'No Diplo'}, {id: 5, name: 'Team'}, {id: 6, name: 'Duel'}, {id: 7, name: 'No War'}];
    let o = {}
    let reType = /!report Game Type:\s+?(\d|\w+)\s?(\w+\s?(\w+)?)?\n/gi
    let match = reType.exec(game)
    if(match[1]){
        for (var j = 0; j < game_types.length; j++) {
            if ((game_types[j].name).toLowerCase() === (match[1]).toLowerCase()) {
                data.type = game_types[j].id;
                break;
            }
        }
    }
    if(match[2]){
        data.moddifier = match[2]
    }
    console.log(data)
    await (data)
}

async function main() {

    console.log("Reading diplo data...\n\r")
    try {
        diplo_data = await gameType(diplo)
        console.log(diplo_data)
    } catch (err) {
        return 'No game type!';
    }        
}

main();
6
  • There is no such thing as a "JSON Object" Commented Sep 15, 2017 at 17:55
  • 2
    You must add return statement before await (data). But the main question is why do you use async/await there? gameType is not asynchronous function. Commented Sep 15, 2017 at 17:56
  • 1
    I'm going to be inserting the data into a database and need to do multiple inserts into different tables so need to do it as a transaction. Commented Sep 15, 2017 at 17:58
  • 1
    So async functions are made for asynchronous code. It looks like all your stuff is synchronous. You don't need to use async functions here. Commented Sep 15, 2017 at 17:59
  • Why are you using async/await? Commented Sep 15, 2017 at 18:14

1 Answer 1

7

To fix the bug in your code, you must add return statement before await (data):

async function gameType(game) {
  ...
  return await (data);
}

But the main question is why do you use async/await there? gameType is not asynchronous function, it doesn't contain any async calls. It should be declared as a regular synchronous function:

function gameType(game) {
   ...
   return data;
}

The function main which calls gameType, should be declared without async too.

function main() {
  console.log("Reading diplo data...\n\r");
  try {
    diplo_data = gameType(diplo);
    console.log(diplo_data);
  } catch (err) {
     return 'No game type!';
  }        
}
Sign up to request clarification or add additional context in comments.

2 Comments

can we use async and await function without returning a promise ?
If a function is declared with async keyword it returns promise in any case, even you explicitly don't return the result.

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.