Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 查找node.js的Promise bluebird代码审查_Javascript_Node.js_Promise_Bluebird - Fatal编程技术网

Javascript 查找node.js的Promise bluebird代码审查

Javascript 查找node.js的Promise bluebird代码审查,javascript,node.js,promise,bluebird,Javascript,Node.js,Promise,Bluebird,何时何地需要使用 我的示例代码: userInfo.js var Promise = require('bluebird'); var winston = require('winston'); var _ = require('lodash'); var request = Promise.promisify(require("request")); exports.getWeather = function (data) { var cityName = data.userProf

何时何地需要使用

我的示例代码: userInfo.js

var Promise = require('bluebird');
var winston = require('winston');
var _ = require('lodash');
var request = Promise.promisify(require("request"));

exports.getWeather = function (data) {
    var cityName = data.userProfile.city;
    return request("http://0.0.0.0:3003/api/Weather/byCity?city=" + cityName).spread(function (res, body) {
        var result = JSON.parse(body).data;
        return _.merge(data, result);
    });
};

exports.getUserProfile = function (userId) {
    return new Promise(function (resolve, reject) {
        request("http://0.0.0.0:3003/api/UserProfile/getUserProfile?id=" + userId).spread(function (res, body) {
            var result = JSON.parse(body).data;
            resolve(result);
        });
    })

};

exports.getEvents = function (data) {
    var cityName = data.userProfile.city;
    return request("http://0.0.0.0:3003/api/Events/byCity?city=" + cityName).spread(function (res, body) {
        var result = JSON.parse(body).data;
        return _.merge(data, result);
    });
};

exports.getFashion = function (data) {
    var gender = data.userProfile.gender;
    return request("http://0.0.0.0:3003/api/Fashion/byGender?gender=" + gender).spread(function (res, body) {
        var result = JSON.parse(body).data;
        return _.merge(data, result);
    });
};

exports.displayDetail = function (data) {
    console.log(data);
};
上面的代码我试着用2种方式调用

getUserProfile.js

var userInfo = require('./userInfo');
    module.exports = function(){
       return userInfo.getUserProfile(3)
                    .then(userInfo.getFashion)
                    .then(userInfo.getEvents)
                    .then(userInfo.getWeather)
                    .then(userInfo.displayDetail)
                    .catch(function (e) {
                        console.log('Error:');
                        console.error(e.stack)
                    })
                    .finally(function () {
                        console.log('done');
                    });

    }
第二种方式:

getUserInformation.js

var userInfo = require('./userInfo');
     module.exports = function () {
        return new Promise(function (resolve, reject) {

             resolve(3);
        })
            .then(userInfo.getUserProfile)
                .then(userInfo.getFashion)
                .then(userInfo.getEvents)
                .then(userInfo.getWeather)
                .then(userInfo.displayDetail)
                .catch(function (e) {
                    console.log('Error:');
                    console.error(e.stack)
                })
                .finally(function () {
                    console.log('done');
                });
    };
getDetails.js

var userInfo = require('./getUserInformation');
    userInfo()
    .then(function(){
            console.log('getDetails done')
        })
        .catch(function (e) {
            console.log('Error:');
            console.error(e.stack)
        })
        .finally(function () {
            console.log('done');
        });

请让我知道使用这些方法有什么区别,还有什么问题吗?

第一种方法更具可读性,而使用第二种方法返回常量的承诺开始链没有任何好处

它们都有效地完成了相同的事情,但有一点需要注意:在第二个示例中(以承诺开始链),调用
getUserProfile
将在下一个勾号上运行(类似于在setTimeout 0中抛出它),而不是以原子方式运行

。只需从回调中
返回
,然后返回由
创建的承诺
,就像在其他三种方法中一样

vs

return new Promise(function (resolve, reject) {
    resolve(3);
})
.then(userInfo.getUserProfile)
.then(…)
嗯,第一个更具可读性和简洁性。除了
getUserProfile
确实同步抛出的情况外,它们几乎是等价的,但无论如何它都不应该同步抛出。同样,在第一种情况下,
getUserProfile
作为
userInfo
上的方法调用,而在第二种情况下,它只是一个回调函数,调用中的
this
将不同

通过使用
Promise.resolve
而不是
newpromise
构造函数,第二种模式可以大大简化:

return Promise.resolve(3)
.then(userInfo.getUserProfile)
.then(…)
这是完全好的,并与链的其余部分更好地对齐。说到这里

其中,每个函数都返回一个与 附加数据合并到其参数中

这并不是解决这个问题的最好办法。是的,它确保这三个函数在彼此之后调用,并且。但是,在您的例子中,您将对API的
请求
调用与参数提取和结果合并在同一个函数中;通过分离关注点,你不应该这样做。而是让函数变得纯粹

exports.… = function (arg) {
    return request("http://0.0.0.0:3003/api/…?…=" + arg).spread(function (res, body) {
        return JSON.parse(body).data;
    });
};
现在,您可以分别组合它们-不仅可以按顺序组合,还可以并行组合:

userInfo.getUserProfile(3)
.then(function(data) {
    var p = data.userProfile;
    return Promise.prop({
         userProfile: 0,
         fashion: userInfo.getFashion(p.gender), // `\
         events: userInfo.getEvents(p.city),     //   }=> execute requests in parallel
         weather: userInfo.getWeather(p.city)    // ./
    });
})
.then(userInfo.displayDetail)
.catch(function (e) {
     console.error('Error:', e.stack)
});

两者之间的区别是什么?什么样的问题?我正在寻找代码审查。。哪种方法是正确的?假设另一种方法是使用node.js样式的回调,那么这两种方法都是正确的。但是创建虚拟的新承诺然后进行链接是正确的吗?对,背景中发生了什么。。当您使用的东西没有实现promise api时,是否存在性能问题。
.then(userInfo.getFashion)
.then(userInfo.getEvents)
.then(userInfo.getWeather)
exports.… = function (arg) {
    return request("http://0.0.0.0:3003/api/…?…=" + arg).spread(function (res, body) {
        return JSON.parse(body).data;
    });
};
userInfo.getUserProfile(3)
.then(function(data) {
    var p = data.userProfile;
    return Promise.prop({
         userProfile: 0,
         fashion: userInfo.getFashion(p.gender), // `\
         events: userInfo.getEvents(p.city),     //   }=> execute requests in parallel
         weather: userInfo.getWeather(p.city)    // ./
    });
})
.then(userInfo.displayDetail)
.catch(function (e) {
     console.error('Error:', e.stack)
});