Hey, Konnor! An interesting take on making a Promise wrapper around otherwise synchronous functions.
I may suggest preserving the original function's (callback) call signature.
awaitasPromise(()=>greet('hi','Konnor'))
This way your asPromise function doesn't have to forward arguments and care about the correct binding of the callback. It becomes focused on the one thing it was written to do: taking a synchronous code and making it async.
Here's the implementation for this call signature:
Also a nitpick: the synchronous logic you're referencing in the examples is not commonly referred to as "callback". Callback is usually referred to functions that are passed as arguments and meant to be executed later when some logic (usually async) settles.
Somehow, the callback pattern was the first thing that came to mind when I read the title of your post:
Hey, Konnor! An interesting take on making a Promise wrapper around otherwise synchronous functions.
I may suggest preserving the original function's (
callback
) call signature.This way your
asPromise
function doesn't have to forward arguments and care about the correct binding of thecallback
. It becomes focused on the one thing it was written to do: taking a synchronous code and making it async.Here's the implementation for this call signature:
Also a nitpick: the synchronous logic you're referencing in the examples is not commonly referred to as "callback". Callback is usually referred to functions that are passed as arguments and meant to be executed later when some logic (usually async) settles.
Somehow, the callback pattern was the first thing that came to mind when I read the title of your post:
I thought we're going to be converting
someFunc
to return a Promise instead of relying on the callback (the second argument).Hey this is the stuff I was looking for! I slapped this together in about 5mins! Didn't even think to just use anonymous functions!