0

I've looked at Promise.all but I need something different, it doesn't matter whether few gets rejected, as long as one or promises are resolved, I'm interested to get hold of the results. Is there a pattern?

Context is http trying to download an image from a remote server and few of the promises may not be successful but I'm interested in get hold of the whatever images I can.

Edit:

I've copied the shim from the page in my code followed by this, is there anything wrong in here?

var p1 = new Promise(function(resolve, reject){
    setTimeout(resolve, 200);
});
var p2 = new Promise(function(resolve, reject){
    setTimeout(resolve, 200);
});
var p3 = new Promise(function(resolve, reject){
    setTimeout(resolve, 300);
});

Promise.settle([p1, p2, p3]).then(function(results){
    console.log('done')
});
user2727195
  • 7,122
  • 17
  • 70
  • 118
  • I don't think there is a pattern that solves your use case. You should write custom implementation of promise management. – Maxim Shoustin Jul 03 '16 at 05:16
  • You can use a custom `Promise.settle()` described here: [ES6 Promise.all() error handle - Is .settle() needed?](http://stackoverflow.com/questions/36605253/es6-promise-all-error-handle-is-settle-needed/36605453#36605453). This will give you all results, resolved or rejected and you can iterate them to collect anything you want. – jfriend00 Jul 03 '16 at 05:17
  • The way you can do it: Create 2 level Promise. 1st level should handle one file download process and it always will return `resolve` but different JSON structure like: `{status:"failed", ...}` or `{status:"succeeded", ...}`. and 2nd level will use `Proimse.all`. So you will aggregate all results no matter if some of files fail – Maxim Shoustin Jul 03 '16 at 05:23
  • @jfriend00 that's not the answer to the question, please unmark as duplicate, I'm not using ES6. – user2727195 Jul 03 '16 at 05:44
  • 1
    @user2727195 - All you need is `Promise.all()` in that answer which you must have if you have any implementation of promises. Did you look at the code in that answer? It does exactly what you want. It tells you when all promises have finished and lets you get all results whether rejected or resolved. That other answer gives you exactly what you want - that's why I marked your question a dup of it. No point in copying the same answer here. – jfriend00 Jul 03 '16 at 05:50
  • please review my edit – user2727195 Jul 03 '16 at 05:57
  • How does the edit change anything? Your code example resolves all promises so you could just use `Promise.all()` for that. Make one of those promises reject and then it would make sense to use the `Promise.settle()` implementation from the other answer I marked yours a dup of. In the future, if you want me to know about comments you post, then please address them to me. Otherwise, it's blind luck whether I ever know they are there. – jfriend00 Jul 03 '16 at 06:44

0 Answers0