0

I am facing an issue in JavaScript. I want to do change promise.all into single promise request.

What should I do? Anyone help me?

It just a sample code for understand:

count: (4) [Promise, Promise, Promise, Promise]

data: (4) [{…}, {…}, {…}, {…}]

//send delivery report with send sms response                                
Promise.all(count).then(function(data) {
    var delivery_reports = [];
    console.log(data);
    SendDeliveryReport(data);
  })
  .catch(function(error) {
    console.log(error);
  })

// execute promises sequentially, passing the parameters from an array

            count.reduce(
            (p, x) =>
                p.then(_ => myPromise(x)),
             //send delivery report with send sms response                                
             Promise.all(count).then(function (data){
               var delivery_reports = [];
                console.log(data);
                SendDeliveryReport(data);
            })
            .catch(function (error){
                console.log(error);
            })
            
            )
adnan khan
  • 101
  • 1
  • 3
  • 7
  • 2
    What is `count`? `SendDeliveryReport`? – Heretic Monkey Sep 04 '20 at 11:58
  • you can check a add count result [@](https://stackoverflow.com/users/215552/heretic-monkey) – adnan khan Sep 04 '20 at 12:01
  • 1
    It's pretty unclear what you're asking still. What's the problem with this current code? What does or doesn't work as expected? What do you want to change? – deceze Sep 04 '20 at 12:02
  • i want to change `promise.all` into `new promise` request [@](https://stackoverflow.com/users/476/deceze) – adnan khan Sep 04 '20 at 12:03
  • i want to send all sms at one time sometime it gave `request timeout error` [@](https://stackoverflow.com/users/476/deceze) – adnan khan Sep 04 '20 at 12:04
  • 1
    How do you expect to change 4 requests into one without `Promise.all`? BTW you should be use `@username` to reply to someone, not a link to the user's profile. – Heretic Monkey Sep 04 '20 at 12:04
  • i want to do handle 4 request one request `one by one save into new array` it is possible [@heretic-monkey](https://stackoverflow.com/users/215552/heretic-monkey) – adnan khan Sep 04 '20 at 12:07
  • First of all, are you talking about the `Promise.all(count)` part or the `SendDeliveryReport(data)` part? We don't know what any of those things do. – deceze Sep 04 '20 at 12:08
  • @adnankhan I don't think your problem is in **Promise.all** Promise.all is resolved when all the Promises are resolved – angel.bonev Sep 04 '20 at 12:08
  • i am talking about `Promise.all(count)` part [@deceze](https://stackoverflow.com/users/476/deceze) – adnan khan Sep 04 '20 at 12:12
  • @adnankhan maybe this will help you.If i'm getting it right [async/await with forEach()](https://codeburst.io/javascript-async-await-with-foreach-b6ba62bbf404) , but you question is not very clear – angel.bonev Sep 04 '20 at 12:13
  • OK, so you have a bunch of promises (`count`), and you want to turn them into one promise (?!), but without `Promise.all`, which is exactly the tool to aggregate many promises into one. … Why? – deceze Sep 04 '20 at 12:18
  • Are you saying that currently you're running all promises (network requests?) in parallel, but that's causing some to timeout, so you want to run them one after the other instead? – deceze Sep 04 '20 at 12:19
  • yes u r right [@deceze](https://stackoverflow.com/users/476/deceze) – adnan khan Sep 04 '20 at 12:30
  • So, this then: https://stackoverflow.com/a/43082995/476 – deceze Sep 04 '20 at 12:36
  • ok thanks i try [@deceze](https://stackoverflow.com/users/476/deceze) – adnan khan Sep 04 '20 at 12:43
  • i try in my code..i add in my code.. can u check it is ok ? [@deceze](https://stackoverflow.com/users/476/deceze) – adnan khan Sep 04 '20 at 13:05

0 Answers0