Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronously serialize api requests in react browser environment? #15

Closed
bfxtsales opened this issue Dec 14, 2021 · 2 comments
Closed

Comments

@bfxtsales
Copy link

Hello. This is not really an issue, rather a question.

Can this library be used to synchronously serialize api requests?

The situation is: Given an array of API URL's to fetch (or axios), with the constraint of only one outstanding api request at a time. How to schedule api requests such that request #2 doesn't get sent until response #1 has been received (or timed out).

Turns out this is easy in the node environment, but I haven't been able to figure out how to do it in the browser environment. (Using create-react-app's built in dev server.)

Please provide example if possible. Or pointer to alternate library if more applicable. Thanks in advance.

@vitaly-t
Copy link
Owner

vitaly-t commented Nov 16, 2022

Sorry for a very late response. I missed a notification from this old project.

This library is all about extra methods for promise resolutions, not about data serialization.

What you are asking is simply achieved via await/async these days.

b.t.w. In my more recent project, iter-ops, it can be achieved like this:

import {pipeAsync, map, wait} from 'iter-ops';

const apis = ['first uri', 'second uri']; // your array of URI-s

const i = pipeAsync(
    apis,
    map(a => fetch(a)), // re-map UIR-s into requests
    wait() // wait for each URI to resolve before making another request
);

(async function() {
    for await (const a of i) {
        console.log(a); // print response from each individual response
    }
})();

And it works in a browser also.

And it can be improved further, through controlled concurrent resolutions, using waitRace operator.

@vitaly-t
Copy link
Owner

I take it's a dead issue now, so closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants