Does the Bluebird card return early?

Suppose I have 10 promises that will be passed in Promise.map()with the default concurrency setting 3.

If any of the first 3 promises is rejected, will another 7 be launched?

The Bluebird documentation for map()reads as follows:

Promises returned by the matching function are expected, and the promised promise is not kept until all displayed promises are fulfilled. If any promise in the array is rejected, or any promise returned by the mapper function is rejected, the returned promise is also rejected.

He only claims that if everything is fulfilled, then internal promises are expected, but it is not clear to me what will happen if any of them are rejected.

Edit

I know that the canonical definition of a function mapis that the length of the output matches the size of the input, but I'm not sure if Bluebird respects this.

+4
source share
2 answers

I got very similar results for @ssube answer.

I got 10 promises, which will be allowed or rejected after increasing the timeout. 4th (because the array is based on 0) is rejected.

const Promise = require('bluebird')

function delay(timeout, err, i) {
  return new Promise(function (resolve, reject) {
    if (err) {
      setTimeout(function () {
        console.log('Rejected', err.message)
        reject(err)
      }, timeout)
    } else {
      setTimeout(function () {
        console.log('Resolved', i)
        resolve(i)
      }, timeout)
    }
  })
}

const promises = Array.apply(null, {length: 10})
  .map(Function.call, Number)
  .map(function (it) {
    if (it === 3) {
      return delay(500 * it, new Error(it))
    } else {
      return delay(500 * it, null, it)
    }
  })

Promise.map(promises, function (p) {
  console.log('Mapping', p)
  return p.toString()
})
  .then(function (it) {
    console.log('All resolved', it)
  })
  .catch(function (err) {
    console.log('Error', err.message)
  })

This will give:

> Resolved 0
> Mapping 0
> Resolved 1
> Mapping 1
> Resolved 2
> Mapping 2
> Rejected 3
> Error 3
> Resolved 4
> Resolved 5
> Resolved 6
> Resolved 7
> Resolved 8
> Resolved 9

So, the behavior is as follows:

  • Promise.map shorted when one of the displayed promises is rejected.
  • A callback is mapnever performed for any subsequent promises.
  • Please note that Error 3precedes intentionally slow promises.
  • promises , .
+2

, Bluebird Promise.prototype.map .

Promise, . , , , :

- 3 promises , 7?

. promises "" ( , ) . 7 , , , , .

, 4 HTTP- 10 . () 3 , , .

, promises .

, , promises, , , - .

:

const Promise = require('bluebird');                    

function delayReject(delay, err) {                      
  return new Promise((res, rej) => {                    
    console.log('waiting to reject', err);              
    setTimeout(() => rej(err), delay);                  
  });                                                   
}                                                       

function delayValue(delay, val) {                       
  return new Promise((res, rej) => {                    
    console.log('waiting to resolve', val);             
    setTimeout(() => res(val), delay);                  
  });                                                   
}                                                       

const promises = [1, 2, 3, 4, 5, 6, 7, 8, 9].map(it => {
  if (it % 3 === 0) {                                   
    return delayReject(50, it);                         
  } else {                                              
    return delayValue(50, it);                          
  }                                                     
});                                                     

Promise.map(promises, v => {                            
  console.log('mapping', v);                            
  return -v;                                            
}).then(it => {                                         
  console.log('mapped', it);                            
}).catch(err => {                                       
  console.log('error', err);    
});                        

node v6.8.1 :

ssube@localhost ~/questions/40619451 $  > node early-exit.js
waiting to resolve 1                                                      
waiting to resolve 2                                                      
waiting to reject 3                                                       
waiting to resolve 4                                                      
waiting to resolve 5                                                      
waiting to reject 6                                                       
waiting to resolve 7                                                      
waiting to resolve 8                                                      
waiting to reject 9                                                       
mapping 1                                                                 
mapping 2                                                                 
error 3 

, promises , map .

Bluebird , :

, . , , , .map concurrency .all.

, , . , :

const Promise = require('bluebird');                    

function delayNoise(n) {                                
  return n + Math.floor(Math.random() * 50);            
}                                                       

function delayReject(delay, err) {                      
  return new Promise((res, rej) => {                    
    console.log('waiting to reject', err);              
    setTimeout(() => rej(err), delayNoise(delay));      
  });                                                   
}                                                       

function delayValue(delay, val) {                       
  return new Promise((res, rej) => {                    
    console.log('waiting to resolve', val);             
    setTimeout(() => res(val), delayNoise(delay));      
  });                                                   
}                                                       

const promises = [1, 2, 3, 4, 5, 6, 7, 8, 9].map(it => {
  if (it % 3 === 0) {                                   
    return delayReject(50, it);                         
  } else {                                              
    return delayValue(50, it);                          
  }                                                     
});                                                     

Promise.map(promises, v => {                            
  console.log('mapping', v);                            
  return -v;                                            
}).then(it => {                                         
  console.log('mapped', it);                            
}).catch(err => {                                       
  console.log('error', err);                            
});                                  

, : , . , .

+5

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


All Articles