Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #33020

Re: synchronizing asynchronus request

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.javascript
Subject Re: synchronizing asynchronus request
Date 2017-04-16 11:09 +0100
Organization A noiseless patient Spider
Message-ID <8760i4pst4.fsf@bsb.me.uk> (permalink)
References <ocv32h$248$1@shakotay.alphanet.ch>

Show all headers | View raw


Une Bévue <unbewusst.sein@fai.invalid> writes:

> I've an app requesting different values to a websocket server :
> albums ;
> cameras ;
> locations ;
> persons ...
>  ...
>
> Then i have answers being asynchronous, but I'd like to synchronize
> all the answers at the time of the latest.
>
> The code is like (ES6) :
>
>     let requestAlbums  = {database: 'photos', ...};
>     this.props.socket.sendRequest(requestAlbums, (message) => {
>       this.setState({albums: message.results.albums});
>     });
>
>     let requestCameras  = {database: 'photos', ...};
>     this.props.socket.sendRequest(requestCameras, (message) => {
>       this.setState({cameras: message.results.cameras});
>     });
>
>     let requestLocations  = {database: 'photos', ...};
>     this.props.socket.sendRequest(requestLocations, (message) => {
>       this.setState({locations: message.results.locations});
>     });
>
>     let requestPersons  = {database: 'photos', ...};
>     this.props.socket.sendRequest(requestPersons, (message) => {
>       this.setState({persons: message.results.persons});
>     });
>
> I'd like to "wait" for the latest result (aka "persons") in order to
> have only one this.setState like :

I think you want to do something when all the results are in.  The last
one in may be the first one requested -- you can't assume that "persons"
will be the last.  (BTW, "latest" does not quite mean what you think it
does -- the "latest result" is not the "last result", it is the result
most recently received.)

One way would be to count (or other record) the results as they come
in.  The handler (the function that currently calls setState) could
increment a result count and call a function to use the result when the
counter becomes 4.

It's not clear why you want only one call to setState because that
function could be doing exactly what I've described -- every time
setState is called it checks if the result that's being recorded is the
last price of the picture.

<snip>
-- 
Ben.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

synchronizing asynchronus request Une Bévue <unbewusst.sein@fai.invalid> - 2017-04-16 08:30 +0200
  Re: synchronizing asynchronus request Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-16 11:09 +0100
    Re: synchronizing asynchronus request Une Bévue <unbewusst.sein@fai.invalid> - 2017-04-16 18:38 +0200
  Re: synchronizing asynchronus request Richard Maher <maher_rjSPAMLESS@hotmail.com> - 2017-04-18 06:32 +0800
    Solved: synchronizing asynchronus request Une Bévue <unbewusst.sein@fai.invalid> - 2017-04-18 11:58 +0200
      Re: Solved: synchronizing asynchronus request Richard Maher <maher_rjSPAMLESS@hotmail.com> - 2017-04-18 21:39 +0800
        Re: Solved: synchronizing asynchronus request Une Bévue <unbewusst.sein@fai.invalid> - 2017-04-19 07:10 +0200
          Re: Solved: synchronizing asynchronus request "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-04-19 12:17 +0200

csiph-web