Hello. I am writing code using Promises to run functions in sequence. Only a couple of days I deal with them to improve the code (for there used to be a Christmas tree from callbacks); So here. There is a function for animating text output to the Animal () screen:
function Animal(string) { var a = ''; //variable which will be entered character by character string var i= 0;//Letter counter var p = document.createElement('p'); $('body').scrollTop($('body').append(p).height()); return new Promise(function (resolve) { Anima(); function Anima() {//Function of Animation a+=string[i]; i++; $('p').last().text(a); var timer = setTimeout(Anima, 100); if(i==string.length){ clearTimeout(timer); resolve(); }; }; }); }; And the AnimalPause () function for the pause image (a line of dots is displayed on the screen and is immediately deleted):
function AnimalPause(string) { var a = ''; //variable which will be entered character by character string var i= 0;//Letter counter var p = document.createElement('p'); $('body').scrollTop($('body').append(p).height()); return promise = new Promise(function (resolve) { Anima(); function Anima() {//Function of Animation a+=string[i]; i++; $('p').last().text(a); var timer = setTimeout(Anima, 100); if(i==string.length){ clearTimeout(timer); $('p').last().remove(); resolve(); }; }; }); }; He did everything, it seems. And, if you output consecutive lines through .then (), everything works. And, if you output the lines sequentially using a loop, everything also works:
Animal('..........') .then(() => Animal('---Hello! CONSOLE v 1.0.1 is working!---')) .then(() => Animal('To see all commands u can use type -help')) .then(() => Animal('DONE')) or so:
var chain = Promise.resolve(); pause.forEach(function(txt){ chain = chain.then(() => AnimalPause(txt))}); var pause = [ '...', '.....', '....', '.........' ]; But if through .then () Sequentially output the text, then the delay, then the text, collapses: (The delay is displayed after the text is displayed):
function Hello() { var chain = Animal('..........') .then(() => Animal('---Hello! CONSOLE v 1.0.1 is working!---')) .then(() => Animal('To see all commands u can use type -help')) .then(function() { return Pause(chain); // pause.forEach(function(txt){ // chain = chain.then(() => AnimalPause(txt))}) }) .then(() => Animal('DONE')) }; Hello(); function Pause (chain) { return new Promise(function(resolve) { pause.forEach(function(txt){ chain = chain.then(() => AnimalPause(txt))}); resolve(); }) } Why? Well .. and how to fix it? :)
On the advice of @Grundy, in the comments I take off the code for testing
var pause = [ '...', '.....', '....', '.........' ]; function Animal(string) { var a = ''; //variable which will be entered character by character string var i = 0; //Letter counter var p = document.createElement('p'); $('body').scrollTop($('body').append(p).height()); return new Promise(function(resolve) { Anima(); function Anima() { //Function of Animation a += string[i]; i++; $('p').last().text(a); var timer = setTimeout(Anima, 100); if (i == string.length) { clearTimeout(timer); resolve(); }; }; }); }; function AnimalPause(string) { var a = ''; //variable which will be entered character by character string var i = 0; //Letter counter var p = document.createElement('p'); $('body').scrollTop($('body').append(p).height()); return promise = new Promise(function(resolve) { Anima(); function Anima() { //Function of Animation a += string[i]; i++; $('p').last().text(a); var timer = setTimeout(Anima, 100); if (i == string.length) { clearTimeout(timer); $('p').last().remove(); resolve(); }; }; }); }; function Pause(chain) { return new Promise(function(resolve) { pause.forEach(function(txt) { chain = chain.then(() => AnimalPause(txt)) }); resolve(); }) }; function Hello() { var chain = Animal('..........') .then(() => Animal('---Hello! CONSOLE v 1.0.1 is working!---')) .then(() => Animal('To see all commands u can use type -help')) .then(function() { return Pause(chain); //Вы заметите, что строка пауз выводится после вывода на экран строки "DONE". Должно быть наоборот }) .then(() => Animal('DONE')) }; Hello(); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
forEachin this case it’s better to usereduce. What kind of pause ? I found Grundy