I have a function that makes a request to an API.
async sendPrompt(prompt: string, engine: string, maxTokens: number): Promise<string> {
const gptResponse = await this.openai.complete({
// ...
// API call details
// ...
});
return gptResponse.data.choices[0].text.substring(1);
}
and another function that calls it, and should return it's response.
queryGPT3(message: string): string {
const prompt = this.preparePrompt('classifyPrompt', message);
const promise = this.sendPrompt(prompt, 'gpt3', 100);
const response = promise.then((res) => {
return res;
}
return response;
}
The final line return response is raising the error Type 'Promise<any>' is not assignable to type 'string'. ts(2322)
I get that the return of an async function is a promise, but I'm calling .then() on it - shouldn't that resolve the promise to an actual value? When I hover over promse.then(), my IDE tells me it's return type is also a promise. Both await and .then() return promises? What's the point of .then()?
How can I get the return of an async function here and store it in a variable?