HardEvent Loop & Promises💻 Output Question

Resolving Nested Promises with Optional Chaining and Nullish Coalescing

💡

Hint

Look into how the promise is resolved and how the optional chaining and nullish coalescing operators are used to access nested properties

What does this output?

function getPokemon() {
  return new Promise((resolve) => {
    resolve({
      name: 'Pikachu',
      abilities: {
        ability1: 'Static',
        ability2: null
      }
    })
  })
}

getPokemon().then((pokemon) => {
  console.log(pokemon.name)
  console.log(pokemon.abilities?.ability2 ?? 'Not found')
  console.log(pokemon.abilities?.ability3 ?? 'Not found')
})

Correct Output

Pikachu
null
Not found

Why this output?

More Event Loop & Promises Output Questions

EasySynchronous code runs before setTimeoutMediumPromise microtask before setTimeout macrotaskMediumPromise chain passes valuesHardTwo Promise chains interleave in microtask queue

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz