How to access the value of a promise?

后端 未结 12 2537
予麋鹿
予麋鹿 2020-11-22 06:55

I\'m looking at this example from Angular\'s docs for $q but I think this probably applies to promises in general. The example below is copied verbatim from the

12条回答
  •  春和景丽
    2020-11-22 07:21

    Maybe this small Typescript code example will help.

    private getAccount(id: Id) : Account {
        let account = Account.empty();
        this.repository.get(id)
            .then(res => account = res)
            .catch(e => Notices.results(e));
        return account;
    }
    

    Here the repository.get(id) returns a Promise. I assign it to the variable account within the then statement.

提交回复
热议问题