Converting a callback to a promise

Sometimes you want your synchronous function to run asynchronously. Perhaps you want to run multiple functions asynchronously using something like Promise.allSettled or Promise.all.

I have a number of setup functions that dont depend on each other in …


This content originally appeared on DEV Community and was authored by DEV Community

Sometimes you want your synchronous function to run asynchronously. Perhaps you want to run multiple functions asynchronously using something like Promise.allSettled or Promise.all.

I have a number of setup functions that dont depend on each other in an application and I was curious how hard it would be to convert the setup functions to async functions without touching their internal code. (Some functions come from libraries)

The TLDR is that yes, I managed to do it.

function asPromise (callback, ...args) {
  return new Promise((resolve, reject) => {
    try {
      resolve(callback(...args))
    } catch(e) {
      reject(e)
    }
  })
}

Now for some examples:

function greet (greeting, name) { return "${greeting}, {name}" } 
await asPromise(greet, "hi", "konnor") 
// => "hi, konnor"

Now what if we pass an object?

function greet ({greeting, name}) { return "${greeting}, {name}" } 
await asPromise(greet, {greeting: "hi", name: "konnor"}) 
// => "hi, konnor"

And finally, what about an array?

function greet (ary) {
  return `${ary[0]}, ${ary[1]}`
}

await asPromise(greet, ["hi", "konnor"])
// => "hi, konnor"

Are there edge cases? Probably. Mostly around this

if your function calls rely on this make sure to bind within the Promise like so:

await asPromise(myFunction.bind(myThis), "arg1")

And that's all for today! Short and sweet.


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2022-03-13T01:45:42+00:00) Converting a callback to a promise. Retrieved from https://www.scien.cx/2022/03/13/converting-a-callback-to-a-promise/

MLA
" » Converting a callback to a promise." DEV Community | Sciencx - Sunday March 13, 2022, https://www.scien.cx/2022/03/13/converting-a-callback-to-a-promise/
HARVARD
DEV Community | Sciencx Sunday March 13, 2022 » Converting a callback to a promise., viewed ,<https://www.scien.cx/2022/03/13/converting-a-callback-to-a-promise/>
VANCOUVER
DEV Community | Sciencx - » Converting a callback to a promise. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/13/converting-a-callback-to-a-promise/
CHICAGO
" » Converting a callback to a promise." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/03/13/converting-a-callback-to-a-promise/
IEEE
" » Converting a callback to a promise." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/03/13/converting-a-callback-to-a-promise/. [Accessed: ]
rf:citation
» Converting a callback to a promise | DEV Community | Sciencx | https://www.scien.cx/2022/03/13/converting-a-callback-to-a-promise/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.