Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I use callback that returns Promise?

I have function:

export const paramsFactory = (params: paramsType) => {
  return ...
}

In another component I have the same function but after return … it also contains await getPageInfo({ page: 1 }).
So I need pass callback as a second param to this function. And how to use it correctly?
I have something like that:

    export const paramsFactory = (params: paramsType, callback?: () => Promise<void>) => {
      return async (...) => { 
        ...
        if (callback) {
          await callback()
        }
      }
    }

await callback() as a part of return statement.
When I use it paramsFactory(params, getPageInfo({ page: 1 })) I get

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TS2345: Argument of type ‘Promise’ is not assignable to
parameter of type ‘() => Promise’.   Type ‘Promise’
provides no match for the signature ‘(): Promise’.

>Solution :

use

paramsFactory(params, () => getPageInfo({ page: 1 }))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading