The below isn't my exact code but I just wanted to outline the structure. When I run the code the sequence I get the console logs is this:
- 'Done'
- json
I was expecting this to be the other way round because in order for function2 to complete (and send the 'Done' resolve value), function3 would have to finish first.
I'd like to understand why it doesn't work that way.
function1().then(() => {
return function2()
).then((message) => {
console.log(message)
})
function function2() {
return new Promise((resolve, reject) => {
fetch(url, {
method: 'get',
body: null,
headers: {
"Content-Type": "application/json; charset=UTF-8",
"Accept": "application/json; charset=UTF-8",
},
})
.then(res => res.json())
.then((json) => {
return function3(json)
})
resolve('Done')
})
}
function function3(json) {
console.log(json)
}