I have an async function that involves an Await Fetch and does some other logic both before and after. The function must return a string at the end, let's say "success" or"failure".
const uspsVerification = async (addr: string) => {
//... Additional Logic ...
await fetch("/service/verify-address",requestOptions)
.then(res => res.json())
.then(data => {
// ... Additional logic ...
if (someCondition1) {
// Return or resolve: "success"
return "success";
// Promise.resolve("success");
} else if {
// Return or resolve: "failure"
return "failure";
// Promise.resolve("failure");
}
});
}
Neither option works. First of all, I can't simply assign a result variable to this function call and check it:
var result = uspsVerification(..);
if (result === 'success') { .. }
Error: Type 'Promise<void>' is not assignable to type 'string'.
Also, I can't use return new Promise(function(resolve, reject) {
around the function body. If I do, I get the error
const uspsVerification = async (addr: string) => {
return new Promise(function(resolve, reject) {
//...
await fetch("/service/verify-address",requestOptions)
//...
resolve(..);
//...
});
Error: 'await' expressions are only allowed within async functions and at the top levels of modules.