I'm learning the world of promise. Everything went well so far, but this time I can't understand why my promise never gets resolved even though it gets throught my conditionnal resolve() (I can see the console.log, but it never gets to the thenable.)
Here is what i'm trying, a typewritter effect that must have dynamic string to type, and wait for one to completed before typing the next one :
<p id="demo"></p>
<p id="demoother"></p>
var zero = 0;
var txt = 'Lorem ipsum dolor sit amet.'; /* The text */
var othertxt = 'Nunc sagittis id tortor non venenatis.';
var container = document.getElementById("demo");
var othrecontainer = document.getElementById("demoother");
var speed = 100; /* The speed/duration of the effect in milliseconds */
function typeWriter(str, node) {
return new Promise(function(resolve) {
console.log(str);
console.log(node.getAttribute('id'));
if (zero < str.length) {
console.log(zero);
node.innerHTML += str.charAt(zero);
zero++;
setTimeout(typeWriter, speed, str, node);
}
if (zero === str.length) {
setTimeout(function () {
resolve(zero);
console.log('resolved');
}, speed);
}
});
}
typeWriter(txt, container).then((value) => {
console.log('then');
typeWriter(othertxt, othrecontainer);
});
I've got the console.log('resolved'), but I never have the console.log('then'). Where am I missing something ? Shouldn't the promise get resolved ?