Asynchronous JS has always been a bit confusing for me...
I have this example code:
function asyncFunction() {
return new Promise(function(resolve, reject) {
resolve([1, 2, 3, 4, 5])
})
};
function example() {
asyncFunction().then(
output => {
for (element of output) {
console.log(element + ", A") //This should be displayed first
}
}
)
};
example();
console.log('B'); //But it isn't
Which produces the following output:
B
1, A
2, A
3, A
4, A
5, A
Is there a way to program this so that B will be printed after the As? I'm actually using an RSS feed parser module here, the above is just an example to illustrate the problem.
console.log('B');would need to be included in thethenfunction because non-async code will always be executed before async code.