I have a bunch of functions used to provide data to my service. I want to loop through each of them and stop as soon as one of them returns the desired result. If the first one works, thats fine. If it has an exception or data is not valid, I would like to move to the next one and so on.
How may I achieve this? I have the below code:
handleData: function(address) {
var self = this;
return new Promise(function (resolve, reject) {
for (var i = 0; i < self.listAllAvailableProviders.length; ++i) {
var handler = self.listAllAvailableProviders[i];
new handler().getData(address)
.then(function(value) {
Logger.info(value);
resolve(value);
})
.catch(function(err){
Logger.error(err);
})
}
reject("");
});
}
how can I fix it to stop as soon as the first one gets the right data? I have read through the bluebirdjs
documentation to no avail.
EDIT
I put a break
statement after resolve
and I got this:
SyntaxError: Illegal break statement
at Object.exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:513:28)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)