Skip to content

Instantly share code, notes, and snippets.

@Mayankgupta688
Created April 24, 2019 11:00
Show Gist options
  • Save Mayankgupta688/1a0f602718b68542e5bc8131cf063024 to your computer and use it in GitHub Desktop.
Save Mayankgupta688/1a0f602718b68542e5bc8131cf063024 to your computer and use it in GitHub Desktop.
import { Observable } from 'rxjs';
var randomNumberGenerator = Observable.create(function(observer) {
setTimeout(function() {
var randomNumber = Math.floor(Math.random() * 100);
if(randomNumber == 13) {
observer.error("Incorrect Random Number Generated");
} else if(randomNumber == 99) {
observer.complete();
} else {
observer.next(randomNumber);
}
}, 1000);
});
randomNumberGenerator.subscribe({
next: function(data) {
console.log("Input Data is: " + data)
}, error: function(errorMessage) {
console.log("Recieved the error with following message: " + errorMessage);
}, complete: function() {
console.log("Observable has completed Execution");
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment