简体   繁体   English

返回一个静态值作为一个承诺

[英]Returning a static value as a promise

In NodeJS I am wrapping a promise function to return a static variable. 在NodeJS中,我包装了promise函数以返回静态变量。 I am sure there is a better way but I couldn't find it on Q's docs . 我相信有更好的方法,但是我在Q的文档中找不到它。 This works however the Q.Promise seems redundant: 这可行,但是Q.Promise似乎是多余的:

function parseXML(filepath) {
    return Q.Promise(function (resolve, reject, notify) {

       readXML(filepath).then(function (xml) {

        ... (long synchronised code)...

        //console.log('products -> ', products);
        resolve(products);

    }).catch(function (err) {
        reject(err);
    });
});
}

In Angular I would use $q.when but within the Q library "when" seems to have a different role, as I understand. 在Angular中,我将使用$ q.when,但据我所知,在Q库中,“ when”似乎起着不同的作用。 I am surely missing some "when" method. 我肯定会错过一些“何时”方法。 This is what I would like to be able to do: 这就是我想要做的:

 function parseXML(filepath) {
    readXML(filepath).then(function (xml) {

        ...

        // some Q method to create a fulfilled promise
        Q.???(products);

    });

}

Both, in Angular and Node, this should be sufficient: 在Angular和Node中,这都足够了:

function parseXML(filepath) {
    return readXML(filepath).then(function(xml) {
        ... (long synchronised code)...
        return products
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM