最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I make a new call to an api when a previous one finished successfully? - Stack Overflow

programmeradmin1浏览0评论

I am new to angular and rxjs, and I have the following scenario, in which I need that after a call to an api is successfully resolved to make a new call, in the context of angular / rxjs I don't know how to do it

handler(): void {
  this.serviceNAme
    .createDirectory(this.path)
    .pipe(
      finalize(() => {
        this.someProperty = false;
      })
    )
    .subscribe(
      (data) => console.log(data),
      (error) => console.error(error.message)
    );
}

What is the correct way to make a new call to an api when a previous one was successful?

I am new to angular and rxjs, and I have the following scenario, in which I need that after a call to an api is successfully resolved to make a new call, in the context of angular / rxjs I don't know how to do it

handler(): void {
  this.serviceNAme
    .createDirectory(this.path)
    .pipe(
      finalize(() => {
        this.someProperty = false;
      })
    )
    .subscribe(
      (data) => console.log(data),
      (error) => console.error(error.message)
    );
}

What is the correct way to make a new call to an api when a previous one was successful?

Share Improve this question edited Nov 26, 2020 at 8:18 frido 14.2k5 gold badges45 silver badges58 bronze badges asked Nov 26, 2020 at 4:18 MarioMario 4,9963 gold badges42 silver badges62 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

I understand you have a serviceOne and a serviceTwo. And you want to call serviceTwo using the retrieved data from serviceOne.

Using rxjs switchMap you can pipe one observable into another.

    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 => {},
            });
    }

If you don't need to pass the data from serviceOne to serviceTwo but you need them to be both pleted together, you could use 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 => {},
        });
    }

Using aysnc and await you can do:

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
}

There are a few says to do this:

Scenario # 1

Your two service api calls are independent, you just want one to go and then the next

 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),
   plete: () => console.log("service call 1&2 plete")
 });

Scenario # 2

Your two calls dependant on one another, so you need the result of the first before you can start the second

 this.serviceName.getDirectoryRoot().pipe(
   switchMap(root => this.serviceName.createDirectoryInRoot(root, this.path))
 ).subscribe({
   next: console.log,
   error: err => console.error(err.message),
   plete: () => console.log("service call 1 used to create service call 2, which is plete")
 });

You'll want scenario # 2, because done this way, an error in the first call will mean no result is sent to the switchMap, and second call is never made.

发布评论

评论列表(0)

  1. 暂无评论