Is it useful to always return a promise

后端 未结 2 1042
无人及你
无人及你 2020-12-06 14:00

I\'m using bluebird to design some nodejs api wrapper around an http service. Many of the functions in this wrapper are asynchronous and so it makes a lot of sense to retur

相关标签:
2条回答
  • 2020-12-06 14:06

    There is no point in returning a promise in synchronous methods.

    Promises provide an abstraction over concurrency. When there is no concurrency involved such as when providing an array. Returning a promise makes for worse flow control and is considerably slower.

    This is also conveying the wrong message. In fact, promisifying things with no reason is quite a common anti pattern.

    One case where it is useful is when a method might be asynchronous - for example: fetching something from cache or making a request for it if it's not there:

    function getData(id){
         if(cache.has(id) return Promise.cast(cache.get(id));
         return AsyncService.fetch(id).tap(cache.put);
    }
    
    0 讨论(0)
  • 2020-12-06 14:25

    If we can ignore perf it's only bad if the example implementation is used:

    function parseArray(someArray) {
        var result;
        // synchronous implementation
        return Promise.resolve(result);           
    }
    

    This function is a mess because it can throw synchronously but also returns a promise. A function that returns a promise must never throw - otherwise you have lost most benefits of promises because now you have to use both try-catch and .catch().

    The correct way to implement is to "annotate" a function with .method:

    var parseArray = Promise.method(function() { 
        var result;
        //Promise.resolve(result) is unnecessary now
        return result;
    });
    

    Now the function is guaranteed to never throw synchronously and can be used in a consistent way.

    0 讨论(0)
提交回复
热议问题