Why does .then behaves async when I pass function inside, but seems to behave synchronously with a method?

Viewed 21

This calls the .then function asynchronously (the 'THEN' string is logged in the last spot of the array)

const array = [];
array.push('before');

new Promise(resolve => {
  array.push('promise');
  resolve();
}).then(()=>array.push('THEN'));

array.push('after');

console.log(array)

//RESULT: ["before", "promise", "after", "THEN"]

But if instead of a function I pass a method .then is called synchronously

const array = [];
array.push('before');

new Promise(resolve => {
  array.push('promise');
  resolve();
}).then(array.push('THEN'));

array.push('after');

console.log(array)

//RESULT: ["before", "constructed", "THEN", "promise"]
0 Answers
Related