简体   繁体   English

Q.all为非承诺和承诺组合

[英]Q.all for a non promise and promise combination

What Am I Doing: I take in an array of svn url and then I perform a checkout operation on all the url. 我在做什么:我先输入svn网址数组,然后对所有网址执行签出操作。

Problem: I am not able to fail the Q.all when the called function doesn't return a promise. 问题:当被调用函数未返回承诺时,我无法使Q.all失败。

Note: Using Q version 2.x 注意:使用Q版本2.x

Source Code: 源代码:

const SvnSpawn = require("svn-spawn");
const Q = require("q");
const validations = require("./validations.js");
const client = new SvnSpawn();

/**
 * Silently checkout the svn content to the specified folder
 * @param {String} svnUrl - URL for the svn checkout
 * @param {String} destPath - Path to the destination folder
 * @returns {void}
 * @private
 */
function checkout(svnUrl, destPath){
    if(validations.isValidString(svnUrl) && validations.isValidString(destPath)){
        const deferred = Q.defer();

        client.cmd(["export", "--force", "--quiet", svnUrl, destPath], (err) => {
            if(err){
                deferred.reject(err);
            }
            else{
                deferred.resolve();
            }
        });
        return deferred.promise;
    }
    else{
        return new Error("SVN: svn url or destination path are not valid");
    }
}

/**
 * Checkout all the svn dependencies
 * @param {String[]} svnDependencies - Collection of SVN dependencies
 * @param {String} workingFolder - Path to the working folder
 * @returns {void}
 * @public
 */
function svnCheckout(svnDependencies, workingFolder){
    const deferred = Q.defer();

    Q.all(svnDependencies.map((svnUrl) =>
            checkout(svnUrl, workingFolder)
        ))
        .then((response) => {
            deferred.resolve();
        })
        .catch((err) => {
            deferred.reject(err);
        });

    return deferred.promise;
}

module.exports.checkout = svnCheckout;

How the above code is consumed: 上面的代码是如何使用的:

const svn = require("./lib/util/svnOperations");

svn.checkout([
    1,
    "http://<url>",
    "http://<url>"
], "./working")
.then(() => {
    console.log("Done");
})
.catch((err) => {
    console.error(err);
})
.done();

Expectation: The above code should result in an error but it exits with Done . 预期:上面的代码应导致错误,但会以Done退出。

让您的代码在失败时返回拒绝的承诺,

return Q.reject(new Error("SVN: svn url or destination path are not valid"));

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

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