I've found lots of questions and answers about promises, but I can't quite wrap my head around how to apply them to my situation. I'm trying to use the result of a promise chain in another function using plain Javascript.
How would I go about doing something like the below?
My expected results is Birthday Greeting Happy birthday to you
Instead, I get Birthday Greeting [object Promise]
HTML
<div id="Heading">Birthday</div>
<p>
<div id="Paragraph"></div>
JS
//Create the phrase
function phrase() {
let p = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Happy ");
}, 3 * 100);
});
var q =
(p.then((result) => {
return result + "birthday "
}).then((result) => {
return result + "to ";
}).then((result) => {
resolve( result + "you");
})
)
return q;
}
//Add to the heading and return the phrase for use by another function
function func1() {
document.getElementById("Heading").innerHTML += "Greeting";
return phrase();
}
//Insert the phrase into the DOM
function func2() {
document.getElementById("Paragraph").innerHTML = func1();
}
//Invoke the whole bit
func2();