nested promises in bluebird

怎甘沉沦 提交于 2019-12-04 03:20:43

问题


I'm trying to figure out how to use promises correctly with the bluebird library. I've come across some nested promises in my code and I noticed that in the bluebird docs it reads:

if you are utilizing the full bluebird API offering, you will almost never need to resort to nesting promises in the first place.

There are many other blog posts about promises being misused and nesting is a regular anti-pattern.

loadCar(someUri) // jqXHR
    .then(function (car) {
        if (carHasFourDoors(car)) {
            loadMake(car.make)
                .then(function (make) {
                    loadModel(make.model)
                        .then(function (model) {
                            loadCarDetails(model)
                        });
                });
        }
        else if (carHasTwoDoors(car)) {
            loadModel(make.model)
                .then(function (model) {
                    loadCarDetails(model)
                });
        }
    });

All of my functions return objects. Looking at the bluebird docs, it seems like there are multiple helper methods: all(), join(), props().

So, my question is: How could I avoid the nesting if there are dependencies? Perhaps this is my misunderstanding of the asynchronous nature of promises. Could something like this work?

Promise.all(loadCar(someUri), loadMake(car.make), loadModel(make.model))
    .then(function(car, make, model) {
        // do logic
    });

回答1:


You always need nesting for control structures, and usually you will need one level of nesting for the function expressions passed to then(). It's not totally avoidable, but can be reduced significantly.

In your case, you even can omit some of the function expressions and pass the functions directly.

loadCar(someUri).then(function (car) {
    if (carHasFourDoors(car)) {
        return loadMake(car.make)
    else if (carHasTwoDoors(car))
        return make; // not sure actually where you get this from
}).then(function (make) {
    return loadModel(make.model)
}).then(loadCarDetails)


来源:https://stackoverflow.com/questions/24412710/nested-promises-in-bluebird

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!