What is the output of the following? “`js let resolve; const p = new Promise(r => { resolve = r; }); p.then(v => console.log(‘resolved:’, v)); resolve(42); console.log(‘sync done’); “`
JavaScript ProfessionalMedium
JavaScript Professional — Medium
What is the output of the following? “`js let resolve; const p = new Promise(r => { resolve = r; }); p.then(v => console.log(‘resolved:’, v)); resolve(42); console.log(‘sync done’); “`
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.