I am trying to run a promise one by one inside forEach.
Sorry, I know this might be a simple question, however I find it difficult to find an answer on how to solve this.
I expect the console.log will show like this
test
a
test
b
test
c
However, it shows like this
test
test
test
a
b
c
This is my code https://jsfiddle.net/brianivander/b6csvrn9/1/
var array1 = ['a', 'b', 'c'];
array1.forEach(function(element) {
promise().then(() => {
console.log(element);
})
});
function promise() {
return new Promise((resolve, reject) => {
console.log('test');
resolve()
})
}
Thanks for your help.