One of my recent favorites is this helper to convert a callback-based JS function to an awaitable:
functionmakeAwaitable(func,callbackArgIndex){/* Return an awaitable version of a callback-based function.
Example:
// Create an awaitable version of setTimeout, specifying the index
// of the expected callback argument index (i.e. 0).
const awaitableSetTimeout = makeAwaitable(setTimeout, 0)
// Invoke the awaitable setTimeout, omitting the callback arg.
await awaitableSetTimeout(1000)
// Returns 1 second later.
*/return(...args)=>newPromise((resolve,reject)=>{// Splice resolve() into args at the callbackArgIndex location. args.splice(callbackArgIndex,0,resolve)// Invoke func, reject the promise on error. try{returnfunc(...args)}catch(e){reject(e)}})}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
One of my recent favorites is this helper to convert a callback-based JS function to an awaitable: