Well, I think you can use a computed property to update your messages and have the right order for both, questions and answers.
I don't understand well if is what you want to achieve, but is what I can understand.
Now, I suggest to identify every message with and Id (messageId) and attach the id to the response, then you can filter the response and put it in place once you get any response in any time.
I did some updates to your jsfiddle and you can see it here (I guess, I don't know very well how it works)
The template, if you want to have message then response, message then response, the better way is to have all in the same v-for statement:
<div class="messagesC" v-for="(data, index) in rightOrder" :key='index'>
<p class="userMessage">
{{ data.message }}
</p>
<p class="botMessage">
{{ data.response }}
</p>
</div>
Now in your methods you can add the Id
...
sendResponse() {
const messageId = Math.random().toString(16).slice(2); // Generate uuid
this.messages.push({
message: this.message,
messageId,
}) // Push message with its id
this.outputResponse(this.message, messageId);
},
outputResponse(input, messageId){ // receives message and messageId
this.message = '';
this.timer = setTimeout(() => {
try {
var output = input + "=" + eval(input);
} catch (e) {
var utterance = [
["hello"],
],intent = [
["hello back!"]
],fallback = ["this is a response to your input"];
var text = (input.toLowerCase()).replace(/[^\w\s\d]/gi, "");
if (this.compare(utterance, intent, text)) {
output = this.compare(utterance, intent, text);
} else {
output = fallback[Math.floor(Math.random() * fallback.length)];
}
}
this.responses.push({
response: output,
messageId
}) // send back the response and the messageId
}, 1000);
}
...
Now the computed property
computed: {
// computed property that searches for an answer
// taking into account the message order
rightOrder() {
return this.messages.map(message => {
const response = {
// Search a response with the messageId
...this.responses.find(
response => response.messageId === message.messageId
)}
// return the message, messageId and its response
return {
message: message.message,
...response
}
})
}
},
And finally the CSS
#window {
float: left;
width: 100%;
}
.userMessage {
flex: flex-shrink;
margin-right: 10px;
}
.botMessage {
flex: flex-shrink;
}
.messagesC {
display: flex;
flex-wrap: wrap;
}
.click {
float: left;
background: black;
color: white;
padding: 0.5%;
}
You can send message without wait for the response, once the responses has a new response, it will be shown right after the message