I'm completely new to Javascript development.
CURRENT PROJECT - At the moment, I am immersed in developing a web chatbot application.
AN ISSUE - I am encountering difficulties using the promise() function to execute more than one function sequentially.
SOURCE CODE -
var messages = [
`Hello there!`,
`Just wanted to let you know that I'm throwing a birthday party this Sunday at my place.`,
`It would mean a lot if you could make it.`,
];
chatWindow = document.querySelector(".message-day");
const startChat = () => {
return new Promise(function (resolve, reject) {
messages.forEach((message) => {
setTimeout(() => {
chatWindow.innerHTML += `
<div class="message">
<div class="message-wrapper">
<div class="message-content">
<h6 class="text-dark">Karan</h6>
<span>${message}</span>
</div>
</div>
<div class="message-options">
<div class="avatar avatar-sm"><img alt="" src="./assets/media/avatar/6.png"></div>
<span class="message-date">9:12am</span>
</div>
</div> `;
}, 2000);
});
resolve();
});
};
startChat().then(() => {
console.log("2nd Function Executed");
});
You can check out the code live here as well.
How can I ensure that the 2nd function runs only after the completion of the startChat() function?