Каков порядок выполнения обещаний в JavaScript
Я хотел бы объяснить себе порядок выполнения следующего фрагмента, который использует обещания JavaScript.
Promise.resolve('A')
.then(function(a){console.log(2, a); return 'B';})
.then(function(a){
Promise.resolve('C')
.then(function(a){console.log(7, a);})
.then(function(a){console.log(8, a);});
console.log(3, a);
return a;})
.then(function(a){
Promise.resolve('D')
.then(function(a){console.log(9, a);})
.then(function(a){console.log(10, a);});
console.log(4, a);})
.then(function(a){
console.log(5, a);});
console.log(1);
setTimeout(function(){console.log(6)},0);
Результат:
1
2 "A"
3 "B"
7 "C"
4 "B"
8 undefined
9 "D"
5 undefined
10 undefined
6
Меня интересует порядок выполнения 1 2 3 7 ... не значения 'A', 'B' ...
Насколько я понимаю, если обещание разрешено, функция 'then' помещается в очередь событий браузера. Так что мое ожидание было 1 2 3 4 ...
@ jfriend00 Спасибо, большое спасибо за подробные объяснения! Это действительно огромный объем работы!