我在做什么:我接受一个 svn url 数组,然后对所有 url 执行 checkout 操作。
问题:当被调用的函数不返回 promise 时,我无法使 Q.all
失败。
注意:使用 Q版本2.x
源代码:
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;
上面的代码是如何使用的:
const svn = require("./lib/util/svnOperations");
svn.checkout([
1,
"http://<url>",
"http://<url>"
], "./working")
.then(() => {
console.log("Done");
})
.catch((err) => {
console.error(err);
})
.done();
预期:上面的代码应该会导致错误,但会以 Done
退出。
最佳答案
让您的代码在失败时返回拒绝的 promise ,
return Q.reject(new Error("SVN: svn url or destination path are not valid"));
关于javascript - Q.all 用于非 promise 和 promise 组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34003010/