Feature Request: Add utility function Promise.first()
See original GitHub issueI’m frequently in a position where I want to say “Try this. If that doesn’t work, try this. If that doesn’t work, try this”. It’d be nice to be able to write:
Promise.first([
method1,
method2,
method3
], {concurrency:1})
It appears that, currently, you can get bluebird to have this desired behavior, but it’s not very clean. I might just be missing a useful pre-existing pattern, though.
Issue Analytics
- State:
- Created 9 years ago
- Reactions:1
- Comments:9 (1 by maintainers)
Top Results From Across the Web
grantila/already: Utility functions for promises - GitHub
The filter function can be called without a promise chain, and act on an array of values or promises as the first argument....
Read more >Resolve promises one after another (i.e. in sequence)?
Instead, we need to create an array of functions that returns a promise. Each function will then be executed sequentially, which then starts...
Read more >How to write a declarative JavaScript promise wrapper
The utility function will accept a promise as the parameter, handle the error internally, and return an array with two elements: resolved value ......
Read more >Keep Your Promises in TypeScript using async/await
Lets see how we can write a Promise and use it in async await . This method helps simplify the code inside functions...
Read more >Using Express.js Routes for Promise-based Error Handling
Router(); const userService = require('../services/userService'); router.get('/', function(req, res) { userService.getAll() .then( ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
An issue with your solution, though, is that it completely ignores any errors or exceptions coming out of the functions. Also, if the resolved result is, itself,
null
, then that would be ignored. I ended up just making a helper function for this, written in coffeescript. In case anyone else finds this useful. Considering how nuanced this issue is and the fact that it is a use case that comes up from time to time, I’m going to go ahead and create that issue after all.@petkaantonov Thank you! I agree. Your usage of
reduce
makes the solution succinct and readable enough. While I feel that it’s a fairly common use-case, I don’t think it’s nearly common enough to necessitate an addition to the API. I’m not going to bother creating a new issue.