less than 1 minute read

また、

あなた

we have a function receiving a callback. can we wrap it into a async function?

ChatGPT

Yes, you can wrap a function that uses a callback pattern into an asynchronous function using async/await by returning a new Promise. This approach allows you to work with callback-based functions in an asynchronous manner, making your code cleaner and easier to read, especially if you're dealing with multiple asynchronous operations.

Here’s a general structure for wrapping a callback-based function into an async function:

…..

ということで、

// Wrap the callback-based function in a promise
function asyncChromeApiWrapper (originalFunctionWithCallback, arg) {
  return new Promise((resolve, reject) => {
    originalFunctionWithCallback(arg, (ret) => {
      resolve(ret)
    })
  })
}

Updated: