How to call a few of the same promises synchronously in a row to fulfill the conditions within the promis?

There is an appeal to a third-party api, for example, send GET api/?name=bla-bal&last_id=
In response to receiving an array of objects [{id: <the item ID>}, ..., {id: <element ID>}]. The number of elements at random in each response.
The first call goes without last_id, the following with the id of the last element of the last sample.
Is counting the number of obtained objects and once the proper number of stop requests to the api.
How to do it?
August 19th 19 at 23:22
2 answers
August 19th 19 at 23:24
Solution
//Your feature request, I just find the randomness, for example
//The value of the default argument was framed for the 1st case, You could be checking
function someAsync(cur = 1){
 return Promise.resolve(cur * Math.random());
}

//Runner function that will asynchronously-recursive perepisyvatsya
function run(next){
 return someAsync(next).then(result => {
 if(result < 0.1) { //Some condition that the latter result is obtained
 return result;
}
 return run(result); //Restart
});
}

run().then(result => console.log('Latest result:', result));
August 19th 19 at 23:26
1 - async await (node 7 --harmony or babel)
async function go(){
 let p1 = promise1 await(...);
 let p2 = await promise2(p1);
 let p3 = await promise3(p2);
 return p3;
}
go().then(response=>{
 console.log(response); // <= p3
}).catch(console.error);

2. You can use the module vo (there is a sequential testing of promises, less convenient.
3. To write something like this:
promise1
.then(response1=>{
 return promise2(response1);
})
.then(response2=>{
 return promise3(response2);
})
.then(response3=>{
 console.log(response3) // <=your result
})
 .catch(console.error);


Of course all functions promise1, promise2, promise3 is this kind of function:
const promise = (params) => new Promise((resolve, reject) => { ..... some code });
Alas, but the number of promises I have not determined. Ie, whether following or not - decides the value of the current of promis. - claire_Herm commented on August 19th 19 at 23:29
: then we have the event model to build
the number of promises in any case limited, and hence the reaction to every promise can be evaluated, and then take Redux and through state run chain of promises. In your case make the object States in the subscription stuff promises to respond to your work, and dispatchit in then all the promises condition and requirement what to do next. Relatively speaking you have a starter promise, who is in everything for your changes the state, that promis that fall under the task receives from the subscription information, and continues the chain. If laziness Mara with reduxa, you can have the same build using EventEmmiter - Roma2 commented on August 19th 19 at 23:32

Find more questions by tags JavaScript