-1

Is there a built-in function to configure a race in the q JavaScript library?

Example:

function fnThatReturnsAPromise1() { /*...*/ }
function fnThatReturnsAPromise2() { /*...*/ }

//Pseudocode: I want to have the first one of these functions that resolves, be the result
q.race([fnThatReturnsAPromise1,fnThatReturnsAPromise2])
.then(function(resultOfWinner) {
  // ...
});

Edit: there appears to be a race method in the source, but it is not mentioned in the documentation. https://github.com/kriskowal/q/blob/v1/q.js#L738

Ben Aston
  • 53,718
  • 65
  • 205
  • 331
  • What do you mean by "configure"??? – Bergi Sep 29 '15 at 12:12
  • @Bergi I have updated my question with an example. – Ben Aston Sep 29 '15 at 12:19
  • Downvoters, please explain, so that I can improve my question. – Ben Aston Sep 29 '15 at 12:24
  • @Kay, it is a race between two asynchronous functions. It is similar to a deliberate race condition. Can you explain how that is not a race? – Ben Aston Sep 29 '15 at 12:25
  • @Kay Why is this not a race? It looks exactly like a race to me (in the sense of `Promise.race`). Here is a related question, although not Q-specific: http://stackoverflow.com/questions/32643593/two-parallel-calls-either-one-succeeds-in-javascript/32644193#32644193. –  Sep 29 '15 at 12:25
  • @BenAston I didn't downvote, but I'd guess they are downvoting this because you could simply check the documentation to answer your question. https://github.com/kriskowal/q/wiki/API-Reference – forgivenson Sep 29 '15 at 12:29
  • @forgivenson I have checked the documentation and could find no mention of it. I also then checked the source code and found a `race` method, hence the question. – Ben Aston Sep 29 '15 at 12:30
  • Perhaps you should explain that in your question then. Mention you are asking about the `race` method you found in the source code (and include the code for it perhaps) – forgivenson Sep 29 '15 at 12:31
  • 1
    Well, the documentation does not show anything that looks like a race, so it does not seem unreasonable ask if there's something he missed, since Q has so many other bells and whistles it's a little bit odd it doesn't have this one, which after all is even in the native Promise spec. Basic principle of dealing with down-voters is to ignore them. If they wanted to explain, they would already have done so in a comment. –  Sep 29 '15 at 12:32
  • @BenAston I just looked at the source, and it does look like there is a `race` function, so why don't you just try using it and see if it is available? – forgivenson Sep 29 '15 at 12:36
  • It seems that code works as is, did you try it? Seems odd that it's not documented though. – Qantas 94 Heavy Sep 29 '15 at 12:36
  • I am using q 0.9.7 and I get an error with the code above to do with the presence of `answerPs`. – Ben Aston Sep 29 '15 at 12:42
  • 1
    That code (if it worked) does not do exactly what you say you want. That code will fulfill **OR** reject the promise when any of the inputs fulfill or reject. If you want something which fulfills on the first fulfilled promise (and presumably ignored rejections?), you'll need to write it yourself anyway. However, I cannot see why the code should not work in the sense of doing what it intends to (which is the same as `Promise.race`). Are you sure you're passing it an array? –  Sep 29 '15 at 12:45
  • @torazaburo OK thanks for the clarification. Actually the rejection behavior you describe is OK in my use-case. – Ben Aston Sep 29 '15 at 17:42

1 Answers1

2

Since v0.9.7 there is a Q.race method that does exactly what you want. It's still not documented yet in the API reference though.

Also, since v1.0.1 the function is available as .race on the ES6-compliant Q.Promise object.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375