I'm trying to synchronize function calls but don't want to use callbacks. I'm still pretty new to this stuff so a little explanation is appreciated
var h1 = $('#title>h1');
$(window).ready(function(){
var s1 = "WELCOME.";
var s2= "Projects";
print(0,s1);
print(0,s2);
});
function print(i,st){
setTimeout(function(){
h1.text(st.substr(0,i));
if(i<8){
print(i+1,st);
}
},250);
}
I did try using Promise from here: How should I call 3 functions in order to execute them one after the other?
but didn't work out. It gave error that result was not found and I couldn't really figure it out.
var h1 = $('#title>h1');
$(window).ready(function(){
var s1 = "WELCOME.";
var s2= "Projects";
new Promise(function(fulfill,reject){
print(0,s1);
fulfill(result);
}).then(function(result){
print(0,s2);
});
});
This is what I got from the stackoverflow question and error that I got was:
Uncaught (in promise) ReferenceError: result is not defined at projects.js:8 at new Promise () at HTMLDocument. (projects.js:6) at j (jquery-3.2.1.min.js:2) at k (jquery-3.2.1.min.js:2)
CODEPEN: https://codepen.io/anon/pen/QaBYQz