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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:48:36  来源:igfitidea点击:

Return value from a promise in Angular

angulartypescriptpromise

提问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 trueor false

问题是它返回了promise对象而不是truefalse

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.logwill just return promise object. StorageProvider's isLoggedmethod should have return true/falseso that the underlying caller will receive value inside .thensuccess callback.

记录承诺使用console.log只会返回承诺对象。StorageProviderisLogged方法应该有 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 promiseAsuccess That will automatically available in promiseBsuccess, 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 promiseAfunction didn't returned anything from its success function, the chained promiseBwould get undefined. The similar thing was happening with you.

从 返回数据promiseA是一项重要的任务,这就是当您返回数据时,底层链式承诺成功回调函数获取该数据的方式。如果假设该promiseA函数没有从其成功函数返回任何内容,则链接的promiseB将获得undefined. 类似的事情也发生在你身上。