简体   繁体   中英

Return new promise Q in ternary condition

Im using the following code which is working OK.

Now I need to return promise resolve but not sure how to do it in this case, there is nice way to do it? please ignore that the code is sync we are working on a tool which every external API method s hould return promise

This is the code

getExtendedFileContent: function(sHTML, aConfig) {
    var oDeferred = Q.defer();
    return aConfig ? this._process(sHTML, aConfig) : sHTML;
},

the this._process(sHTML, aConfig) & the sHTML should return

oDeferred.promise;

Don't use Q.defer . Just use the Q function (or Promise.resolve if you're working with an ES6-compatible promise libary):

function getExtendedFileContent(sHTML, aConfig) {
    return Q(aConfig ? this._process(sHTML, aConfig) : sHTML);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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