You execute all your queries in parallel in a for loop, so when you find one that has a value that you like, the others are already running, so there is no way to “not run” them. If you do not want to run the rest as soon as you find them, you do not need to run them in parallel. Thus, this will lead you to a design pattern where you will serialize queries. Run it, if it fails, run the next one, etc.
As far as I can tell, there is no built-in circuit in Bluebird to accomplish what you ask. The simplest thing I can think of is to use one of the array processing functions in Bluebird, which will serialize the queries one after another, like Promise.mapSeries() , and then use the reject to abort processing when you find a good value.
handleData: function(address) { return Promise.mapSeries(this.listAllAvailableProviders, function(handler) { return new handler().getData(address).then(function(value) {
Curiously, this is apparently less code, and perhaps a little easier if you just iterate over the handlers yourself:
handleData: function(address) { var index = 0; var handlers = this.listAllAvailableProviders; var handlerCnt = handlers.length; function next() { if (index < handlerCnt) { var handler = handlers[index++]; return new handler().getData(address).catch(next); } else { return Promise.reject(new Error("No handler found for address")); } } return next(); }
source share