There is a function that handles response from scan:
export const handleScanResponse = (
scanResponse: IScanResponse,
visitors: IVisitor[]
): Promise<IVisitor | any> => {
return new Promise((resolve, reject) => {
if (!scanResponse || scanResponse.errorDesc !== "SUCCESS")
throw new Error("Scane response is empty!");
const found = visitors.find((p: IVisitor) => {
if (scanResponse && "qrcode" in scanResponse) {
return (
p.code && p.code.toLowerCase() === scanResponse.qrcode.toLowerCase()
);
} else {
return (
p.document_number &&
scanResponse.document_number &&
p.document_number.toLowerCase() ===
scanResponse.document_number.toLowerCase()
);
}
});
if (found) resolve(found);
reject(scanResponse);
}).catch((e) => console.log(e));
};
I tried to resolve, reject
in cases when user was found and not found in array. Otherwise try to throw an exeption.
Using is:
handleScanResponse(
scanerResponseMockup,
this.visitorsService.visitors$.getValue()
).then(
(foundVisitor) => this.visitorWasFound(foundVisitor),
(scanResponse) => this.visitorNotFound(scanResponse)
);
How to do that properly? Why despite exception I always go to .then( (foundVisitor) => this.visitorWasFound(foundVisitor)
?