typescript 从 Angular 中的承诺返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45649750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return value from a promise in Angular
提问by Tasos
I am a little bit confused with Promises. I have the following provider in Ionic + Angular:
我对 Promises 有点困惑。我在 Ionic + Angular 中有以下提供程序:
@Injectable()
export class StorageProvider {
constructor( public storage:Storage ) {}
save(keyname: string, data: any): Promise<any> {
return this.storage.set(keyname, data);
}
retrieve(keyname: string): Promise<any> {
return this.storage.get(keyname);
}
clear(): Promise<void> {
return this.storage.clear();
}
delete(keyname): Promise<any> {
return this.storage.remove(keyname);
}
isLogged() {
return this.retrieve('user').then( value => { value ? true : false });
}
}
And in one of my components:
在我的组件之一中:
console.log(this.storage.isLogged());
The problem is that it returns the promise object and not true
or false
问题是它返回了promise对象而不是true
或false
If I change my component to:
如果我将组件更改为:
this.storage.isLogged().then(function(data) { console.log(data)});
Then I get an output of undefined
然后我得到一个输出 undefined
采纳答案by Pankaj Parkar
Logging promise using console.log
will just return promise object. StorageProvider
's isLogged
method should have return true/false
so that the underlying caller will receive value inside .then
success callback.
记录承诺使用console.log
只会返回承诺对象。StorageProvider
的isLogged
方法应该有 returntrue/false
以便底层调用者将在.then
成功回调中接收值。
isLogged() {
return this.retrieve('user').then( value => {
//this return will `return` value in chained manner
return value ? true : false;
});
}
Caller
呼叫者
this.storage.isLogged().then(
(value) => console.log("isLogged", value)
)
Why inner return statement solved the problem?
为什么内部返回语句解决了问题?
Promise is way to deal async request, and it basically has 3 function that can appear in .then
.
Promise 是处理异步请求的方式,它基本上有 3 个可以出现在.then
.
myPromiseCall().then(successFn, errorFn, notifyFn)
Sweet properties of promise is they can easily chained up. So that interdependent async code looks more readable. So whenever there is case of passing a data of one promise to another, so that time you returns a data from promiseA
success That will automatically available in promiseB
success, for eg. below
承诺的甜蜜属性是它们可以很容易地链接起来。这样相互依赖的异步代码看起来更具可读性。因此,每当有将一个承诺的数据传递给另一个承诺的情况时,这样您promiseA
就可以从成功返回数据这将在promiseB
成功时自动可用,例如。以下
promiseA(){return promise };
promiseB(){ return promise };
promiseA().then(
//success fn
(data) => {
//do something
return data;
}
).then(
//success fn
(promiseAData) => {
console.log("Promise A data", promiseAData);
//do some awesome thing based on data retrieved from promiseA
return promiseB();
}
).then(successFn, errorFn)
It was important task to return a data from promiseA
, that is how when you returned a data, the underlying chained promise success callback function got that data. If suppose the promiseA
function didn't returned anything from its success function, the chained promiseB
would get undefined
. The similar thing was happening with you.
从 返回数据promiseA
是一项重要的任务,这就是当您返回数据时,底层链式承诺成功回调函数获取该数据的方式。如果假设该promiseA
函数没有从其成功函数返回任何内容,则链接的promiseB
将获得undefined
. 类似的事情也发生在你身上。