2024-03-23 警察のご厄介にThanks a lot
また、
あなた
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 newPromise
. 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) }) }) }