MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at master 99 lines 3.1 kB view raw
1// Test void operator with promises and async functions 2console.log('=== Void Operator Tests ==='); 3 4// Test 1: void with async function (fire and forget) 5console.log('\nTest 1: void with async function'); 6async function asyncTask() { 7 console.log('Async task executing'); 8 return 'task result'; 9} 10 11// Using void indicates we don't care about the result/promise 12void asyncTask(); 13console.log('After void async call'); 14 15// Test 2: void with Promise.resolve 16console.log('\nTest 2: void with Promise.resolve'); 17void Promise.resolve(42).then(v => console.log('Promise resolved with: ' + v)); 18console.log('After void Promise.resolve'); 19 20// Test 3: void with regular function call 21console.log('\nTest 3: void with regular function'); 22function regularFunc() { 23 console.log('Regular function called'); 24 return 'regular result'; 25} 26const result = void regularFunc(); 27console.log('Result of void is: ' + result); 28 29// Test 4: void in expression 30console.log('\nTest 4: void in expression'); 31const x = 10; 32const y = void x; 33console.log('void x where x=10 is: ' + y); 34 35// Test 5: void with multiple expressions 36console.log('\nTest 5: void prevents await'); 37async function noAwait() { 38 // void means we explicitly don't want to await this 39 void Promise.resolve('not awaited'); 40 console.log('Continued without awaiting'); 41 return 'done'; 42} 43noAwait().then(v => console.log('noAwait returned: ' + v)); 44 45// Test 6: void vs await comparison 46console.log('\nTest 6: void vs await comparison'); 47async function withAwait() { 48 const result = await Promise.resolve('awaited value'); 49 console.log('With await: ' + result); 50 return result; 51} 52 53async function withVoid() { 54 void Promise.resolve('void value'); 55 console.log('With void: undefined (not waiting)'); 56 return 'immediate return'; 57} 58 59withAwait().then(v => console.log('withAwait result: ' + v)); 60withVoid().then(v => console.log('withVoid result: ' + v)); 61 62// Test 7: void with chained promise (fire and forget) 63console.log('\nTest 7: void with promise chain'); 64void Promise.resolve(1) 65 .then(v => v + 1) 66 .then(v => v + 1) 67 .then(v => console.log('Chained promise result: ' + v)); 68console.log('Promise chain started but not awaited'); 69 70// Test 8: void return value 71console.log('\nTest 8: void always returns undefined'); 72function returnVoid() { 73 return void 100; 74} 75const voidResult = returnVoid(); 76console.log('Function returning void 100: ' + voidResult); 77 78// Test 9: void with function expression 79console.log('\nTest 9: void with function expression'); 80void function() { 81 console.log('IIFE with void executed'); 82}(); 83 84// Test 10: void indicating intentional non-handling 85console.log('\nTest 10: void for intentional non-handling'); 86async function backgroundTask() { 87 return await Promise.resolve('background work done'); 88} 89 90async function mainTask() { 91 // void explicitly shows we don't want to wait for backgroundTask 92 void backgroundTask(); 93 console.log('Main task continues immediately'); 94 return 'main done'; 95} 96 97mainTask().then(v => console.log('mainTask result: ' + v)); 98 99console.log('\n=== Synchronous code finished ===');