Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get data from API in Custom function #1119

Closed
callmehero opened this issue Dec 7, 2022 · 6 comments
Closed

Get data from API in Custom function #1119

callmehero opened this issue Dec 7, 2022 · 6 comments
Labels
Question Further information is requested

Comments

@callmehero
Copy link

Description

I am using runfunction in FunctionPlugin for showing API result. but it not working as expected due to return executed before the api response.

SO I used to promise inside the fn param but it showing this error

ERROR:
Argument of type ‘(result: any) => Promise’ is not assignable to parameter of type ‘(…arg: any) => InterpreterValue’.
Type ‘Promise’ is not assignable to type ‘InterpreterValue’.ts(2345)

Steps to reproduce

return this.runFunction(

ast.args,

state,

this.metadata("VLOOKUPSHEETFN"),

(param) => {

let item = this.paramFn();

console.log("skjdbvds", item);

return new Promise((resolve, reject) => {
return item = Api call result
});

return Hello , ${item}!;

},

);

Demo

Your environment

angular 13

  • HyperFormula version: 12.0.1 (handsontable version)
  • Browser Name and version: chrome latest
  • Operating System: Windows

Links

@callmehero callmehero added the Bug Something isn't working label Dec 7, 2022
@sequba
Copy link
Contributor

sequba commented Dec 7, 2022

Hi, @callmehero. Thanks for your feedback. HyperFormula does not support asynchronous custom functions at its current development stage. We discussed this idea a while ago, but at the moment, we prioritize bug and interoperability fixes, so this feature is not on our immediate roadmap.

But, as @warpech wrote in #9 (comment):

right now, the easiest way to achieve synchronicity is in the user code outside HyperFormula. I mean a two-step strategy, in which a function like SHARE_PRICE("AMZN") synchronously resolves to a value from cache (or gives an error if the cache is empty) while asynchronously fetching the current value. When fetching is done, update the cache and reevaluate getCellValue/getSheetValues.

In your case, I think you can try this approach:

  • Fetch data from API every X minutes and store it in the Javascript object (cache).
  • Custom function VLOOKUPSHEETFN reads data from the cache object and returns it.
  • Refresh HyperFormula every time a cache value change is detected. 2 things might be helpful for that:

Does it make sense for your use case?

@sequba sequba added Question Further information is requested and removed Bug Something isn't working labels Dec 7, 2022
@sequba
Copy link
Contributor

sequba commented Dec 13, 2022

Closing. @callmehero, if you have any further questions related to this issue, reopen it, please.

@sequba sequba closed this as completed Dec 13, 2022
@abhii-bit
Copy link

I have a question related to this:

I want

Hi, @callmehero. Thanks for your feedback. HyperFormula does not support asynchronous custom functions at its current development stage. We discussed this idea a while ago, but at the moment, we prioritize bug and interoperability fixes, so this feature is not on our immediate roadmap.

But, as @warpech wrote in #9 (comment):

right now, the easiest way to achieve synchronicity is in the user code outside HyperFormula. I mean a two-step strategy, in which a function like SHARE_PRICE("AMZN") synchronously resolves to a value from cache (or gives an error if the cache is empty) while asynchronously fetching the current value. When fetching is done, update the cache and reevaluate getCellValue/getSheetValues.

In your case, I think you can try this approach:

  • Fetch data from API every X minutes and store it in the Javascript object (cache).

  • Custom function VLOOKUPSHEETFN reads data from the cache object and returns it.

  • Refresh HyperFormula every time a cache value change is detected. 2 things might be helpful for that:

Does it make sense for your use case?

Thanks sequba - would there be a way to reference local data in a custom formula in React? I was trying to follow the documentation - wasn't sure how to reference variables in the formula that were not the input variables.

Grateful for your assistance.

@AMBudnik
Copy link
Contributor

Hi @abhii-bit! @sequba answered the same subject here #808 (reply in thread)
If you'd have any further questions, please let us know.

@sequba
Copy link
Contributor

sequba commented Apr 12, 2024

@abhii-bit @callmehero We have just released HyperFormula 2.7.0 which adds a context configuration parameter that can be used to reference local data in a custom formula.

@sandzone
Copy link

The current solution only deals with async cells that do not depend on other async cells. If this were to be integrated with Handsontable, is there a hook that gets invoked after rebuildAndRecalculate() is done?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants