question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Add utility function Promise.first()

See original GitHub issue

I’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:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
timhuffcommented, Feb 4, 2015

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.

Promise.first = (args..., methods=[], thisArg)->
    if methods.length == 0
        return Promise.resolve()
    errors = new Promise.AggregateError
    firstResolvedMethod = (n=0)->
        methods[n].apply thisArg, args
        .error (error)->
            errors.push error
            if n < methods.length-1
                return firstResolvedMethod n+1
            else
                return Promise.reject errors
    return firstResolvedMethod()
0reactions
timhuffcommented, Feb 4, 2015

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found