Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #33018 > unrolled thread
| Started by | Une Bévue <unbewusst.sein@fai.invalid> |
|---|---|
| First post | 2017-04-16 08:30 +0200 |
| Last post | 2017-04-19 12:17 +0200 |
| Articles | 8 — 4 participants |
Back to article view | Back to comp.lang.javascript
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
| From | Une Bévue <unbewusst.sein@fai.invalid> |
|---|---|
| Date | 2017-04-16 08:30 +0200 |
| Subject | synchronizing asynchronus request |
| Message-ID | <ocv32h$248$1@shakotay.alphanet.ch> |
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 :
let albums = message.results.albums;
let cameras = message.results.cameras;
let locations = message.results.locations;
let persons = message.results.persons;
this.setState({
albums: albums,
cameras: cameras,
locations: locations,
persons: persons
});
albums, cameras, locations and persons being "memorized/bufferized"
somewhere...
[toc] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2017-04-16 11:09 +0100 |
| Message-ID | <8760i4pst4.fsf@bsb.me.uk> |
| In reply to | #33018 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Une Bévue <unbewusst.sein@fai.invalid> |
|---|---|
| Date | 2017-04-16 18:38 +0200 |
| Message-ID | <od06mt$2ci$1@shakotay.alphanet.ch> |
| In reply to | #33020 |
Le 16/04/2017 à 12:09, Ben Bacarisse a écrit : > 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. yes, i admit that; your solution is easy to implement. I've to verify the order of results versus the order of requests. > 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. I want only one setState (I'm using Inferno framework) because otherwise I have a render () for each setState the states before everythings are received are meaningless.
[toc] | [prev] | [next] | [standalone]
| From | Richard Maher <maher_rjSPAMLESS@hotmail.com> |
|---|---|
| Date | 2017-04-18 06:32 +0800 |
| Message-ID | <od3fqe$1bi1$1@gioia.aioe.org> |
| In reply to | #33018 |
On 16-Apr-17 2:30 PM, Une Bévue wrote: > 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. > Could Promises help you here? https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
[toc] | [prev] | [next] | [standalone]
| From | Une Bévue <unbewusst.sein@fai.invalid> |
|---|---|
| Date | 2017-04-18 11:58 +0200 |
| Subject | Solved: synchronizing asynchronus request |
| Message-ID | <od4o09$og4$1@shakotay.alphanet.ch> |
| In reply to | #33032 |
Le 18/04/2017 à 00:32, Richard Maher a écrit :
>>
> Could Promises help you here?
>
> https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
>
Yes, u'r right !
I did it in the mean time [test in ES5]:
var promises = [];
var albumsPromise = new Promise(function(resolve, reject){
var albumsRequest = {database: 'photos', collection: 'albums', mode:
'read', sort: {startdate: 1, stopdate: 1}};
socketService.sendRequest(albumsRequest, function (message) {
resolve({
albums_delay: ((new Date()).getTime() - start),
albums_count: message.results.albums.length, albums:
message.results.albums
});
});
});
promises.push(albumsPromise);
the like for cameras, locations, person etc.
and then :
Promise.all(promises).then(function(datas) {
here I have an Array of object, then i re-arrange to a sole object by :
var data = {promises_count: promises.length};
for (var i = 0, l = datas.length; i < l; i++) {
for (var prop in datas[i]) {
data[prop] = datas[i][prop];
}
}
});
here (ie; with Promise.all) the delay is roughly the longest delay.
I have tested another solution like :
var albumsTrick = function () { return new Promise(blahblahblah);};
and then : albumsTrick.then(camerasTrick)...
in that case, if n is the number of socket requests, the time delay is
at least n * (a delay roughly 50 ms)
[toc] | [prev] | [next] | [standalone]
| From | Richard Maher <maher_rjSPAMLESS@hotmail.com> |
|---|---|
| Date | 2017-04-18 21:39 +0800 |
| Subject | Re: Solved: synchronizing asynchronus request |
| Message-ID | <od54v2$1pt8$1@gioia.aioe.org> |
| In reply to | #33043 |
On 18-Apr-17 5:58 PM, Une Bévue wrote: > Le 18/04/2017 à 00:32, Richard Maher a écrit : >>> >> Could Promises help you here? >> >> https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/all >> >> > > Yes, u'r right ! > Glad it worked. What I used to do, and may well do again, is instantiate and object/closure as a counting semaphore that is only referenced by one instance of each of your asynch routines. When the limit or total is reached a final callback is called. I'm still ambivalent about promises but they appear to be trying to usurp asynch and can be easier to read I guess.
[toc] | [prev] | [next] | [standalone]
| From | Une Bévue <unbewusst.sein@fai.invalid> |
|---|---|
| Date | 2017-04-19 07:10 +0200 |
| Subject | Re: Solved: synchronizing asynchronus request |
| Message-ID | <od6rgc$pll$1@shakotay.alphanet.ch> |
| In reply to | #33044 |
Le 18/04/2017 à 15:39, Richard Maher a écrit :
> What I used to do, and may well do again, is instantiate and
> object/closure as a counting semaphore that is only referenced by one
> instance of each of your asynch routines. When the limit or total is
> reached a final callback is called.
I think there is no neeed of a counter with [ES6]:
Promise.all(promises).then( (datas) => {
// here i get datas in the form of an array :
// datas = [data_1, data_2, data_3, ...]
// with each data_n corresponding to the resolve of one promise, in
the order of the promises.
});
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2017-04-19 12:17 +0200 |
| Subject | Re: Solved: synchronizing asynchronus request |
| Message-ID | <XnsA75C7D186D91Feejj99@194.109.6.166> |
| In reply to | #33052 |
=?UTF-8?Q?Une_B=c3=a9vue?= <unbewusst.sein@fai.invalid> wrote on 19 Apr 2017
in comp.lang.javascript:
> I think there is no neeed of a counter with [ES6]:
>
> Promise.all(promises).then( (datas) => {
> // here i get datas in the form of an array :
> // datas = [data_1, data_2, data_3, ...]
> // with each data_n corresponding to the resolve of one promise, in
> the order of the promises.
>});
I thought, one of the main attributes of promises was that they might be
broken later on. ;-)
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web