[Solved] Unable to use await in an if function


Your issue starts with question 4. You are trying to run code without waiting for the return.

if (positionInput.content.toLowerCase() === 'community agent') {
    // Won't wait
    call.prompt(question5CommunityString, {
        time: 50000,
        channel: usersDMs
    }).then(question5CMsg => {
        question5Answer = question5CMsg.content; // Won't run we likely already left the scope without waiting for the return
    });

    // Won't wait
    call.prompt(question6CommunityString, {
        time: 50000,
        channel: usersDMs
    }).then(question6CMsg => {
        question5Answer = question6CMsg.content; // Won't run we likely already left the scope without waiting for the return
    });
}

// This will likely run before the code above completes and will send the moderators.
if (positionInput.content.toLowerCase() === 'moderator') {
    positionChoice = positionInput.content;
    call.messge.author.send('moderators')
}

Try pulling the code outside of the then statement in the question 4 otherwise you need to chain the promises one after another.

// Put this somewhere safe up top
let question4Response;

// Your new code:

    then(positionInput => {
        question4Response = positionInput;
    });
}

if (question4Response.content.toLowerCase() === 'community agent') {
    await call.prompt(question5CommunityString, {
        time: 50000,
        channel: usersDMs
    }).then(question5CMsg => {
        question5Answer = question5CMsg.content;
    });

    await call.prompt(question6CommunityString, {
        time: 50000,
        channel: usersDMs
    }).then(question6CMsg => {
        question6Answer = question6CMsg.content;
    })
}
else if (question4Response.content.toLowerCase() === 'moderator') {
    positionChoice = question4Response.content;

    call.messge.author.send('moderators')
}

0

solved Unable to use await in an if function