如何使用Q.Promise <string>作为字符串?

时间:2015-06-24 01:10:21

标签: typescript promise q

我有函数返回字符串Q.Promise

module test {

    export function promiseString (): Q.Promise<string> {
        var deferred = Q.defer<string>();
        deferred.resolve('someMessage');
        return deferred.promise;
    }

}

如何将返回值用作字符串?

选项#1:

Q.fcall(promiseString).then(function (message: string) {
    //  compilation error about incompatible parameters     
});

选项#2:

Q.fcall(promiseString).then(function (promise: Q.Promise<string>) {
    console.log(typeof promise);    //  output string
    //  but i can't use promise as string further
});

1 个答案:

答案 0 :(得分:4)

如果函数总是返回一个promise(延迟或其他),那么你不必用Q.fcall来调用它。只需致电:

promiseString().then(...)