I have a function that I am trying to call which has below signature
public async submit(
value: string,
callbackFn: (response: string[]) => never | void
): Promise<void>
I understand that this function returns a promise of void
, but one of the values of the signature is a callback that contains the response in it.
I am calling that function like this
export const getResponse = async (
service: Service,
input: string
): Promise<string[]> => {
try {
const result = await service.submit(
input,
(response) => {
return response;
}
);
return Promise.resolve(result as string[]);
} catch (error) {
console.log(error);
throw error;
}
};
When debugging, I realized that even with setting await service.submit(...)
the code execution continues and the caller to export const getResponse
receives undefined
value.
Is there a way I can force the code to not return until there is a response from the callback?
public async submit(
value: string,
callbackFn: (response: string[]) => void
): Promise<string[]> {
return new Promise<string[]>((resolve, reject) => {
setTimeout(() => {
try {
const response = [value, "response"];
callbackFn(response);
resolve(response);
} catch (error) {
reject(error);
}
}, 1000);
});
}
// Define the getResponse function
export const getResponse = async (
service: Service,
input: string
): Promise<string[]> => {
try {
const result = await service.submit(
input,
(response) => {
console.log("Callback response:", response);
}
);
return result;
} catch (error) {
console.log(error);
throw error;
}
};
Parthil Shekhada is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2