JavaScript Professional — Medium
Explanation
The correct answer is "sync done, resolved: 42" because the Promise is resolved with the value 42 before the "sync done" message is logged. This demonstrates the asynchronous nature of Promises in JavaScript.
Ready to go further?
Related questions
- What is the output of the following? ```js async function foo() { try { await Promise.reject(new Error('oops')); } catch(e) { console.log(e.message); } } foo(); ```
- What is the content-security-policy implication of using eval() or new Function() in JavaScript?
- What is tree shaking in JavaScript module bundlers and what syntax enables it?
- What is the output of the following? ```js class EventEmitter { #handlers = {}; on(event, handler) { (this.#handlers[event] ??= []).push(handler); } emit(event, data) { this.#handlers[event]?.forEach(h => h(data)); } } const ee = new EventEmitter(); ee.on('test', v => console.log(v * 2)); ee.emit('test', 5); ee.emit('missing', 5); ```
- What is the difference between `Promise.any()` and `Promise.race()`?
- What is the output of the following? ```js const obj = {}; obj.a = 1; Object.defineProperty(obj, 'b', {value: 2, enumerable: false}); const copy = {...obj}; console.log(copy); ```
