加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular – APP_INITIALIZER – Promise vs Observable

发布时间:2020-12-17 08:34:13 所属栏目:安全 来源:网络整理
导读:我有基于Angular v4的应用程序.场景很简单 – 我需要在应用程序启动之前从服务器加载一些设置.为此,我使用APP_INITIALIZER: { provide: APP_INITIALIZER,useFactory: init,deps: [SettingsService],multi: true}export function init(config: SettingsServi
我有基于Angular v4的应用程序.场景很简单 – 我需要在应用程序启动之前从服务器加载一些设置.为此,我使用APP_INITIALIZER:
{
  provide: APP_INITIALIZER,useFactory: init,deps: [SettingsService],multi: true
}

export function init(config: SettingsService) {
   return () => config.load_one();
}

//import declarations    
@Injectable()
export class SettingsService {
  constructor(private http: HttpClient) { }

  load_one(): Promise<boolean> {
    return new Promise<boolean>((resolve) => {
        this.http.get('url').subscribe(value => {
            console.log('loadSettings FINISH');
            resolve(true);
        });
    });
  }

  load_two(): Observable<any> {
    const promise = this.http.get('url');

    promise.subscribe(value => {
        console.log('loadSettings FINISH');
    });

    return promise;
  }
}

在应用程序的某个地方,我有一个名为manageSettings()的函数(其代码目前无关紧要),这需要初始化来自SettingsService服务的数据.

事情就是这样 – 当我使用函数load_two()时,应用程序不会等到它完成:

app-root constructor
manageSettings()
loadSettings FINISH

当我使用函数load_one()时它工作正常:

loadSettings FINISH
app-root constructor
manageSettings()

谁能解释为什么会这样?

load_one工作和load_two的原因并不简单:Angular只会等待Promises;不是观察者.

在查看docs,博客文章,教程等时,很难找到关于APP_INITIALIZER是否支持返回Observable的明确答案.很明显它确实支持Promises – e.g.:

Angular will delay the initialization if the function returns a promise until the promise resolution.

为了确保,您实际上可以看到Angular code正在做什么:

if (isPromise(initResult)) {
    asyncInitPromises.push(initResult);
}

其中isPromise定义如下:

export function isPromise(obj: any): obj is Promise<any> {
    // allow any Promise/A+ compliant thenable.
    // It's up to the caller to ensure that obj.then conforms to the spec
    return !!obj && typeof obj.then === 'function';
}

APP_INITIALIZER代码中没有对Observable的引用;只是承诺.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读