#wrapper for function.bind

26 messages · Page 1 of 1 (latest)

delicate ermine
#

I create Next server actions while binding them (important) and I want to make this process simpler for me

This is what I initially had:

async function _signInUser(credentials: { login: string; password: string }) {
    // some obscure and eerie manipulations 21+
    return { credentials }
  }
}

// actual function that I use in code (binding lets me use this func in client components)
export async function signInUser(credentials: { login: string; password: string }) {
  return _signInUser.bind(null, credentials)()
}

I then removed the first declaration and used it anonymously (it worked):

export async function boundServerAction(_credentials: { login: string; password: string }) {
  return (async (credentials: typeof _credentials) => {
    // some obscure and eerie manipulations 21+
    return { credentials }
  }).bind(null, _credentials)()
}

Now I am spinning wheels in my brain trying to create a general purpose bindServerAction function that will get an async callback as the 1st param and return Promise of what this callback itself was supposed to return:

export const boundServerAction = bindServerAction(async (name: string) => { return name })
// boundServerAction: (name: string) => Promise<string>

Any ideas?

strange ivy
#
function bindServerAction<Args extends unknown[], C extends (...args: Args ) => Promise<unknown>>(callback: C, ...args: Args) {
  return callback.call(null, ...args)
}
#

something like that?

#

Or do you want to return a bound function? You said you want to return a promise of what the call returns, which is the code above

#

I didnt't test, might be a mistake or two

delicate ermine
#

I want to return a bound function, as if it was function #2 from the 1st codeblock, signInUser

strange ivy
#
function bindServerAction<Args extends unknown[], C extends (...args: Args ) => Promise<unknown>>(callback: C, ...args: Args) {
  return callback.bind(null, ...args)
}
delicate ermine
#

I need bindServerAction function to only accept a callback

strange ivy
#

Hmm it complains about this typing

strange ivy
#

Can the bound function be called with args?

delicate ermine
#

no, it must have args

strange ivy
#

You need to explain clearly

delicate ermine
#

I explained it above

strange ivy
#
function bindServerAction<
    C extends (this: null, ...args: never) => Promise<unknown>
>(callback: C) {
  return callback.bind(null)
}
#

This doesn't take args when binding, but can take args later on

#

You want to generate a function that works like

export async function signInUser(credentials: { login: string; password: string }) {
  return _signInUser.bind(null, credentials)()
}

?

delicate ermine
#

yes

strange ivy
#

But that functions takes all of it's arguments. You don't need a function to create it, you just make a generic function

delicate ermine
strange ivy
#

arg so many similar

strange ivy
#

.call(null) is like .bind(null)()

#

I put it some type assertion for the return type, because TS was being dumb

meager muralBOT
#
sandiford#0

Preview:```ts
function bindServerAction<
Args extends unknown[],
C extends (...args: Args) => Promise<unknown>

(callback: C, ...args: Args): ReturnType<C> {
return callback.call(null, ...args) as ReturnType<C>
}

const o = {
async do() {
console.log("this is",
...```