Skip to content

Commit 8834d46

Browse files
authored
Merge pull request #3 from manthanank:new-changes
feat: updated
2 parents 2b1cbe6 + 103bd7c commit 8834d46

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

README.md

+44-1
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,53 @@ export class App implements OnInit {
175175
bootstrapApplication(App);
176176
```
177177

178-
**bindCallback** -
178+
**bindCallback** - `bindCallback` operator is used to convert a callback-style function into an observable.
179+
180+
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.
179181

180182
```typescript
183+
import 'zone.js/dist/zone';
184+
import { Component, OnInit } from '@angular/core';
185+
import { CommonModule } from '@angular/common';
186+
import { bootstrapApplication } from '@angular/platform-browser';
187+
import { bindCallback } from 'rxjs/ajax';
181188

189+
@Component({
190+
selector: 'my-app',
191+
standalone: true,
192+
imports: [CommonModule],
193+
template: `
194+
<h1>bindCallback Example</h1>
195+
<button (click)="performAsyncOperation()">Perform Async Operation</button>
196+
`,
197+
})
198+
export class App implements OnInit {
199+
200+
ngOnInit() {}
201+
202+
performAsyncOperation() {
203+
const boundAsyncFunction = bindCallback(this.someAsyncFunction);
204+
205+
boundAsyncFunction().subscribe(([error, result]) => {
206+
if (error) {
207+
console.error('An error occurred:', error);
208+
} else {
209+
console.log('Result:', result);
210+
}
211+
});
212+
}
213+
214+
someAsyncFunction(callback: (error: any, result: any) => void) {
215+
// Simulating an asynchronous operation
216+
setTimeout(() => {
217+
const error = null;
218+
const result = 'Hello, world!';
219+
callback(error, result);
220+
}, 2000);
221+
}
222+
}
223+
224+
bootstrapApplication(App);
182225
```
183226

184227
**bindNodeCallback** -

0 commit comments

Comments
 (0)