I have node.js web app with such code
import request from 'request';
export const promisedRequest = (options: any): any => {
const deferred = new Promise((resolve: any, reject: any) => {
return request(options, (error: any, response: any, body: any) => {
if (error || !(response.statusCode >= 200 && response.statusCode <= 208)) {
reject({ error: error || response });
} else {
resolve({ response, body });
}
});
});
return deferred;
};
and it calls in this way
export const getProduct = (token: string, barcode: string): Promise<any> =>
promisedRequest({
url: `${process.env.API_URL}`,
method: 'POST',
json: true,
headers: {
'X-Token': token,
},
body: {
barcode: barcode,
},
})
.then(({ body }: any) => body.data[0])
.catch((error: any): void => {
const errorMessage = `Cannot get product: Product barcode: ${barcode}`;
console.error(errorMessage, error);
throw Error(errorMessage);
});
I checked the browser and requests send right but if I make requests almost at the same time (~2 milliseconds) I get the same response for two users. Please help me with this. Thank you very much.