AutomaApp / automa

A browser extension for automating your browser by connecting blocks
https://www.automa.site
Other
11.44k stars 1.23k forks source link

long waiting time fetching feature of automaFetch #1818

Open HuangKaibo2017 opened 4 weeks ago

HuangKaibo2017 commented 4 weeks ago

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.

jingbof commented 4 weeks ago

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.

HuangKaibo2017 commented 1 week ago

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