第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

當(dāng)上一個(gè) api 成功完成后,如何對(duì) api 進(jìn)行新的調(diào)用?

當(dāng)上一個(gè) api 成功完成后,如何對(duì) api 進(jìn)行新的調(diào)用?

慕森卡 2023-09-07 17:02:40
我是 Angular 和 rxjs 的新手,我有以下場(chǎng)景,在該場(chǎng)景中,我需要在成功解析對(duì) api 的調(diào)用后進(jìn)行新的調(diào)用,在 Angular / rxjs 的上下文中我不知道該怎么做它handler(): void {  this.serviceNAme    .createDirectory(this.path)    .pipe(      finalize(() => {        this.someProperty = false;      })    )    .subscribe(      (data) => console.log(data),      (error) => console.error(error.message)    );}當(dāng)上一個(gè) api 成功調(diào)用時(shí),重新調(diào)用 api 的正確方法是什么?
查看完整描述

3 回答

?
素胚勾勒不出你

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊

我知道你有 aserviceOne和 a?serviceTwo。并且您想serviceTwo使用從 檢索到的數(shù)據(jù)進(jìn)行調(diào)用serviceOne。

使用 rxjs?switchMap您可以將一個(gè)可觀察值通過(guò)管道傳輸?shù)搅硪粋€(gè)可觀察值中。

? handler(): void {

? ? ? ? this.serviceOne

? ? ? ? ? ? .createDirectory(this.path)

? ? ? ? ? ? .pipe(

? ? ? ? ? ? ? ? switchMap(serviceOneResult => {

? ? ? ? ? ? ? ? ? ? // transform data as you wish

? ? ? ? ? ? ? ? ? ? return this.serviceTwo.methodCall(serviceOneResult);

? ? ? ? ? ? ? ? })

? ? ? ? ? ? )

? ? ? ? ? ? .subscribe({

? ? ? ? ? ? ? ? next: serviceTwoResult => {

? ? ? ? ? ? ? ? ? ? // here we have the data returned by serviceTwo

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? error: err => {},

? ? ? ? ? ? });

? ? }

如果您不需要從serviceOne到傳遞數(shù)據(jù)serviceTwo,但需要它們一起完成,則可以使用 rxjs?forkJoin。

? handler(): void {

? ? ? ? forkJoin([

? ? ? ? ? ? this.serviceOne.createDirectory(this.path),?

? ? ? ? ? ? this.serviceTwo.methodCall()

? ? ? ? ])

? ? ? ? .subscribe({

? ? ? ? ? ? next: ([serviceOneResult, serviceTwoResult]) => {

? ? ? ? ? ? ? ? // here we have data returned by both services

? ? ? ? ? ? },

? ? ? ? ? ? error: err => {},

? ? ? ? });

? ? }


查看完整回答
反對(duì) 回復(fù) 2023-09-07
?
翻翻過(guò)去那場(chǎng)雪

TA貢獻(xiàn)2065條經(jīng)驗(yàn) 獲得超14個(gè)贊

使用aysncandawait你可以這樣做:


async handler(): void {

  await this.serviceNAme

    .createDirectory(this.path)

    .pipe(

      finalize(() => {

        this.someProperty = false;

      })

    )

    .subscribe(

      (data) => console.log(data),

      (error) => console.error(error.message)

    );


   // Do second api call

}


查看完整回答
反對(duì) 回復(fù) 2023-09-07
?
一只甜甜圈

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊

有一些說(shuō)法可以做到這一點(diǎn):


場(chǎng)景#1

您的兩個(gè)服務(wù) api 調(diào)用是獨(dú)立的,您只想調(diào)用一個(gè),然后調(diào)用下一個(gè)


 const serviceCall1 = this.serviceName.createDirectory(this.path);

 const serviceCall2 = this.serviceName.createDirectory(this.otherPath);


 concat(serviceCall1 , serviceCall2).subscribe({

   next: console.log,

   error: err => console.error(err.message),

   complete: () => console.log("service call 1&2 complete")

 });

場(chǎng)景#2

您的兩個(gè)調(diào)用相互依賴(lài),因此您需要第一個(gè)調(diào)用的結(jié)果才能開(kāi)始第二個(gè)調(diào)用


 this.serviceName.getDirectoryRoot().pipe(

   switchMap(root => this.serviceName.createDirectoryInRoot(root, this.path))

 ).subscribe({

   next: console.log,

   error: err => console.error(err.message),

   complete: () => console.log("service call 1 used to create service call 2, which is complete")

 });

您將需要方案 # 2,因?yàn)檫@樣做,第一次調(diào)用中的錯(cuò)誤將意味著沒(méi)有結(jié)果發(fā)送到switchMap,并且永遠(yuǎn)不會(huì)進(jìn)行第二次調(diào)用。


查看完整回答
反對(duì) 回復(fù) 2023-09-07
  • 3 回答
  • 0 關(guān)注
  • 165 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)