automa icon indicating copy to clipboard operation
automa copied to clipboard

long waiting time fetching feature of automaFetch

Open HuangKaibo2017 opened this issue 1 year ago • 2 comments

Is your feature request related to a problem? Please describe. I am composing a feature calling cloudflase model rest api by automaFetch as below. The rest api returns like 20 sec but automaFetch returns much earlier even with .then or await. so variable llm_response cannot be set in "automaSetVariable('llm_response', res.response);".

async function fetchAndProcess() { try { const result = await automaFetch('json', { url: url, method: 'POST', headers: { 'Authorization': Bearer ${apiKey}, 'Content-Type': 'application/json', }, body: JSON.stringify({ prompt: 'tell me a story', }), });

const res = await result;
console.log(`result: ${JSON.stringify(result, null, 2)}`);
if (res.success) {
  // Set the variable in automa and log the response
  automaSetVariable('llm_response', res.response); // Set the returned value
  console.log(`res: ${JSON.stringify(res.response, null, 2)}`);
} else {
  console.error('Request failed:', res.message || 'Unknown error');
}

} catch (error) { console.error('Fetch Error:', error); // Handle any fetch or processing errors } }

// Call the async function fetchAndProcess();

Describe the solution you'd like It would be good that adding an timeout parameter to automaFetch and make sure the function would return with data or return after timeout.

Describe alternatives you've considered Pls give a proper sample before timeout parameter added.

Additional context Add any other context or screenshots about the feature request here.

HuangKaibo2017 avatar Sep 09 '24 02:09 HuangKaibo2017

Might be caused by the double-awaiting -- automaFetch returns a promise, and you already await the result with const result = await automaFetch(...), so const res = await result; is incorrect because result is not a promise anymore -- it's the resolved value.

Just remove const res = await result; and replace the references to res with result directly.

jingbof avatar Sep 10 '24 05:09 jingbof

@jingbof how to reuse the promise in my js code created by automaFetch or other system function?

HuangKaibo2017 avatar Sep 28 '24 10:09 HuangKaibo2017