[英]Q.all for a non promise and promise combination
我在做什么:我先輸入svn網址數組,然后對所有網址執行簽出操作。
問題:當被調用函數未返回承諾時,我無法使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
退出。
讓您的代碼在失敗時返回拒絕的承諾,
return Q.reject(new Error("SVN: svn url or destination path are not valid"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.