Store response in const to manipulate it

Hi everyone

This is my first time using Apollo client. I’m having hard time manipulating data received from the client.

I’ve a const on a page called “price”. Which equals to 10, let’s say. In another file, I’m getting the response from client server “1”. Now, I need to store response from client “1” in a variable (const/let), export it, import it to page where I’ve “price” and divide it by price. So 1 / 10. And then display that as final result to the user.

I’m honestly lost as to how I can achieve this.

Hey @zkzkzk :wave:

To make sure I’m answering this correctly, are you trying to do something like the following?

// file-a.ts
const { data } = await client.query(query);

export const divisor = data.value;

// file-b.ts
import { divisor } from './file-a';

const price = 10;
const result = price / divisor;

If this is what your’e asking, I actually think what you’re struggling with here is a limitation of JavaScript. To my knowledge, there is no way to export an asynchronous value from one module to another without it being wrapped in an async function. This blog post has some good examples and limitations that discuss this issue.

That being said, I’d question how you’re structuring your code. Since price is the constant in this case, I’d recommend exporting that value instead, then importing it in the file where you load the data and show the value to the user here.

// file-a.ts
export const price = 10;

// file-b.ts
import { price } from './file-a';

// NOTE: This uses top-level await, which may not be available in your environment
const { data } = await client.query(query);
const result = data.value / price;

// show result to the user

Is there something preventing you from structuring your code in this way?