是否有快捷方式来定义和返回被拒绝的承诺?

时间:2016-02-15 23:59:00

标签: javascript promise q

为了始终从我的函数返回一个promise,我要么从api调用返回promise,要么生成一个deferred对象,然后拒绝它,然后返回它的承诺......

function listIssues(user, repository, filters) {

    if (!user || !repository) {

        // this seems a bit long winded, is there an easier way..?
        var deferred = Q.defer();
        var warning = helper.chalk.gray('You must specifiy user/organization and repository name...');
        deferred.reject(warning);
        return deferred.promise;

    } else {

        return api.getIssues(user, repository, filters);

    }

}

有一条捷径可以一步完成所有这些吗?

0 个答案:

没有答案