[Edit: this is a question about Promises, not setTimeout. Please don't close it without actually reading the question]
I'm trying to understand Promises. From what I've read and understood (which is obviously incomplete or wrong), the following should wait 3 seconds, append "in foo", then append "in bar".
However, "in bar" is first, followed by a 3 second wait, followed by "in foo".
I could use a shove in the right direction on this.
function promiseTest() {
let promise = new Promise(function(resolve, reject) {
setTimeout(foo, 3000);
});
promise.then(foo).then(bar());
}
function foo() {
jQuery('#demo').append("<li>in foo</li>");
}
function bar() {
jQuery('#demo').append("<li>in bar</li>");
}
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<ul id='demo'></ul>
<button onclick='promiseTest()'>click</button>
</body>
</html>