Skip to content

Commit

Permalink
feat: Update ajax & bindCallback examples in README
Browse files Browse the repository at this point in the history
  • Loading branch information
manthanank committed Jun 6, 2024
1 parent b6939a8 commit 54ec50e
Showing 1 changed file with 17 additions and 49 deletions.
66 changes: 17 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ Operators are functions. There are two kinds of operators:

**ajax** - It creates an observable for an Ajax request with either a request object with url, headers, etc or a string for a URL.

Example with Angular:

```typescript
import 'zone.js/dist/zone';
import { Component, OnInit } from '@angular/core';
Expand All @@ -163,71 +165,35 @@ import { ajax } from 'rxjs/ajax';
})
export class App implements OnInit {
ngOnInit() {
const githubUsers = `https://api.github.com/users?per_page=2`;

const users = ajax(githubUsers);

const subscribe = users.subscribe(
(res) => console.log(res),
(err) => console.error(err)
);
const githubUsers = ajax('https://api.github.com/users?per_page=2');
githubUsers.subscribe((res) => console.log(res.status, res.response));
}
}

bootstrapApplication(App);
```

**bindCallback** - `bindCallback` operator is used to convert a callback-style function into an observable.
[Stackblitz Angular Example Link](https://stackblitz.com/edit/angular-ees5qn?file=src%2Fmain.ts)

It allows you to work with asynchronous APIs that follow the Node.js-style callback pattern, where the last argument of a function is a callback function that is invoked with the result or error.
Example with TypeScript:

```typescript
import 'zone.js/dist/zone';
import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { bootstrapApplication } from '@angular/platform-browser';
import { bindCallback } from 'rxjs/ajax';

@Component({
selector: 'my-app',
standalone: true,
imports: [CommonModule],
template: `
<h1>bindCallback Example</h1>
<button (click)="performAsyncOperation()">Perform Async Operation</button>
`,
})
export class App implements OnInit {

ngOnInit() {}
import { ajax } from 'rxjs/ajax';

performAsyncOperation() {
const boundAsyncFunction = bindCallback(this.someAsyncFunction);
const githubUsers = ajax('https://api.github.com/users?per_page=2');
githubUsers.subscribe((res) => console.log(res.status, res.response));
```

boundAsyncFunction().subscribe(([error, result]) => {
if (error) {
console.error('An error occurred:', error);
} else {
console.log('Result:', result);
}
});
}
[Stackblitz TypeScript Example Link](https://stackblitz.com/edit/rxjs-umrlkx?file=index.ts)

someAsyncFunction(callback: (error: any, result: any) => void) {
// Simulating an asynchronous operation
setTimeout(() => {
const error = null;
const result = 'Hello, world!';
callback(error, result);
}, 2000);
}
}
**bindCallback** - `bindCallback` operator is used to convert a callback-style function into an observable.

bootstrapApplication(App);
```
It allows you to work with asynchronous APIs that follow the Node.js-style callback pattern, where the last argument of a function is a callback function that is invoked with the result or error.

**bindNodeCallback** - `bindNodeCallback` is a function that converts a Node.js-style callback function into an Observable.

Example with Angular:

```typescript
import 'zone.js/dist/zone';
import { Component, OnInit } from '@angular/core';
Expand Down Expand Up @@ -282,6 +248,8 @@ export class App implements OnInit {
bootstrapApplication(App);
```

[Stackblitz Angular Example Link](https://stackblitz.com/edit/stackblitz-starters-avcder?file=src%2Fmain.ts)

**defer** - Creates an Observable that, on subscribe, calls an Observable factory to make an Observable for each new Observer.

```typescript
Expand Down

0 comments on commit 54ec50e

Please sign in to comment.