Why does my Promises array work before Promise.all () is called?

I am trying to create an array of Promises and then resolve them using Promise.all (). I use got, which returns a promise.

My code works, but I do not quite understand how to do this. There he is:

const got = require('got');

const url = 'myUrl';
const params = ['param1', 'param2', 'param3'];

let promiseArray = [];
for (param of params) {
    promiseArray.push(got(url + param));
}

// Inspect the promises
for (promise of promiseArray) {
    console.log(JSON.stringify(promise));
    // Output: promise: {"_pending":true,"_canceled":false,"_promise":{}}
}

Promise.all(promiseArray).then((results) => {
     // Operate on results - works just fine
}).catch((e) => {
    // Error handling logic
});

What pushes me away is that Promises are marked as “pending” when I add them to the array, which means that they have already started.

I think that they should lie inactive in promiseArray, but Promise.all(promiseArray)will run them and allow.

Does this mean that I start them twice?

+4
source share
4 answers

. Promises , , JS . , .

Promise.all() , ( ). Promise.all() / .

+4

Promises . .

, :

promiseArray.push(got(url + param));

got() , , .

Promise.all() promises , . "" - . async promises. , async, async.


, :

let promiseArray = [];
for (param of params) {
    promiseArray.push(got(url + param));
}

got() , async . got() , promiseArray. , async .

// Inspect the promises
for (promise of promiseArray) {
    console.log(JSON.stringify(promise));
    // Output: promise: {"_pending":true,"_canceled":false,"_promise":{}}
}

promises, , - , , async .

Promise.all(promiseArray).then((results) => {
     // Operate on results - works just fine
}).catch((e) => {
    // Error handling logic
});

, Promise.all(), promises, , , .

+5

Promises "start", , .. , , ( ) , . , HTTP-, HTTP-, -.

, , (got) , API, API HTTP Request/Response. ( , got), , API. HTTP- API HTTP, .

, promises , "" "". . - API, , , , , then .

+1

, URL- Promise.all :

  • - URL- , ( , .
  • If your array is very large, you will hide the site and your network using requests, you can block the maximum open requests and / or requests made in a certain timeframe.

The first problem is easily resolved; you process failed requests and add them to the results. In the permission handler, you can decide what to do with failed requests:

const got = require('got');

const url = 'myUrl';
const params = ['param1', 'param2', 'param3'];

const Fail = function(details){this.details = details;};
Promise.all(
  params.map(
    param =>
      got(url + param)
      .then(
        x=>x,//if resolved just pass along the value
        reject=>new Fail([reject,url+param])
      )
  )
).then((results) => {
  const successes = results.filter(result=>(result && result.constructor)!==Fail),
  const failedItems = results.filter(result=>(result && result.constructor)===Fail);
}).catch((e) => {
    // Error handling logic
});

Point 2 is a bit more complicated, throttling can be performed using this helper function and will look something like this:

... other code
const max5 = throttle(5);
Promise.all(
  params.map(
    param =>
      max5(got)(url + param)
      .then(
        x=>x,//if resulved just pass along the value
        reject=>new Fail([reject,url+param])
      )
  )
)
+1
source

Source: https://habr.com/ru/post/1690207/


All Articles