Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
Last active July 11, 2024 18:04
Show Gist options
  • Save ninmonkey/50f61ead27e691bb211d0e46e3587642 to your computer and use it in GitHub Desktop.
Save ninmonkey/50f61ead27e691bb211d0e46e3587642 to your computer and use it in GitHub Desktop.
Silly Excessive Example of calling Synchronous function as async in javascript

Example 2

🟠 fnSync => exit: πŸš€ Sync before Async all()
🟒 fnAsync => enter: πŸ’
🟠 fnSync => exit: πŸ’
🟒 fnAsync => enter: 🐈
🟠 fnSync => exit: 🐈
🟠 fnSync => exit: πŸš€ Sync after Async all(), "after" is read as in air quotes
'[ SyncResult: πŸš€ Sync after Async all(), "after" is read as in air quotes ]'
resolveAfter: 1, πŸ™Š invoked early
resolveAfter: 300, πŸ™Š
resolveAfter: 1200, πŸ™Š last to invoke
Final Results: πŸ™Š last to invoke,[ SyncResult: πŸ’  ],πŸ™Š,[ SyncResult: 🐈 ],πŸ™Š invoked early
🟠 fnSync => exit: πŸ±β€πŸ‰ manual sync after promise list, x = πŸ™Š last to invoke,[ SyncResult: πŸ’  ],πŸ™Š,[ SyncResult: 🐈 ],πŸ™Š invoked early
Promise.all() => end. x = [ SyncResult: πŸ±β€πŸ‰ manual sync after promise list, x = πŸ™Š last to invoke,[ SyncResult: πŸ’  ],πŸ™Š,[ SyncResult: 🐈 ],πŸ™Š invoked early ]

Example 1

fnSync => exit: πŸš€ Sync before Async all()
fnSync => exit: πŸ’
fnSync => exit: πŸš€ Sync after Async all(), "after" is read as in air quotes
'πŸ“Œ returned by fnSync: πŸš€ Sync after Async all(), "after" is read as in air quotes'
resolveAfter: 10, SpeedyFirst
resolveAfter: 700, first1?
resolveAfter: 700, first2?
resolveAfter: 1200, SlowLast
Final Results: πŸ“Œ returned by fnSync: πŸ’ ,SlowLast,first1?,first2?,SpeedyFirst
fnSync => exit: πŸ±β€πŸ‰ manual sync after promise list, x = πŸ“Œ returned by fnSync: πŸ’ ,SlowLast,first1?,first2?,SpeedyFirst
Promise.all() => end. x = πŸ“Œ returned by fnSync: πŸ±β€πŸ‰ manual sync after promise list, x = πŸ“Œ returned by fnSync: πŸ’ ,SlowLast,first1?,first2?,SpeedyFirst
function fnSync ( params ) {
// some Sync function that returns something
console.log( `🟠 fnSync => exit: ${ params }` )
return `[ SyncResult: ${ params } ]` // renders nicer in the log than
// return { syncResult: 'didStuff', params: params }
}
function fnAsync ( params ) {
// now it's async
return new Promise( ( resolve ) => {
console.log( `🟒 fnAsync => enter: ${ params }` )
resolve( fnSync( params ) )
} )
}
function asyncDelay ( params, delay ) {
delay ??= 700
return new Promise( ( resolve ) => {
setTimeout( () => {
console.log( `resolveAfter: ${ delay ?? '' }, ${ params ?? '' }` )
resolve( params )
}, delay )
} )
}
fnSync( 'πŸš€ Sync before Async all() ' )
stuff = [
asyncDelay( `πŸ™Š last to invoke`, 1200 ),
fnAsync( 'πŸ’ ' ),
asyncDelay( `πŸ™Š`, 300 ),
fnAsync( '🐈' ),
asyncDelay( `πŸ™Š invoked early`, 1 ),
]
Promise.all( stuff )
.then( ( x ) => {
console.log( `Final Results: ${ x }` )
return fnSync( `πŸ±β€πŸ‰ manual sync after promise list, x = ${ x }` )
//return fnAsync( `πŸ±β€πŸ‰ manual async after promise list, x = ${ x }` )
} )
.then( ( x ) => console.log( `Promise.all() => end. x = ${ x }` ) )
fnSync( 'πŸš€ Sync after Async all(), "after" is read as in air quotes' )
function fnSync ( params ) {
// some Sync function that returns something
console.log( `fnSync => exit: ${ params }` )
return `SyncResult: ${ params }` // return { syncResult: 'didStuff', params: params }
}
function fnAsync ( params ) {
// now it's async
return new Promise( (resolve) => {
resolve( fnSync( params ) )
})
}
fnSync( 'πŸš€ Sync before Async all() ' )
stuff = [ fnAsync( 'πŸ’ '), fnAsync( '🐈' ) ]
Promise.all( stuff )
.then( (x) => {
console.log( `Accumulated Responses: ${ x }` )
return fnSync( `πŸ±β€πŸ‰ manual sync after promise list, x = ${ x }`)
//return fnAsync( `πŸ±β€πŸ‰ manual async after promise list, x = ${ x }` )
})
.then( (x) => console.log( `Promise.all() => end. x = ${ x }` ) )
fnSync( 'πŸš€ Sync after Async all(), "after" is read as in air quotes' )
function fnSync ( params ) {
// some random Sync function
// console.log( `fnSync => enter: ${ params }` )
// ...
console.log( `fnSync => exit: ${ params }` )
return `πŸ“Œ returned by fnSync: ${ params }`
}
function fnAsync ( params ) {
return new Promise( (resolve) => {
resolve( fnSync( params ) )
})
}
function resolveAfter( params, delay ) {
delay ??= 700
return new Promise((resolve) => {
setTimeout(() => {
console.log( `resolveAfter: ${ delay ?? '' }, ${ params ?? '' }` )
resolve( params );
}, delay );
});
}
fnSync( 'πŸš€ Sync before Async all() ' )
stuff = [
fnAsync( 'πŸ’ '),
resolveAfter( 'SlowLast', 1200 ),
resolveAfter( 'first1?' ),
resolveAfter( 'first2?' ),
resolveAfter( 'SpeedyFirst', 10 ),
]
Promise.all( stuff )
.then( (x) => {
console.log( `Final Results: ${ x }` )
return fnSync( `πŸ±β€πŸ‰ manual sync after promise list, x = ${ x }`)
//return fnAsync( `πŸ±β€πŸ‰ manual async after promise list, x = ${ x }` )
})
.then( (x) => console.log( `Promise.all() => end. x = ${ x }` ) )
fnSync( 'πŸš€ Sync after Async all(), "after" is read as in air quotes' )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment