Files
api/docs/examples/rx/04_unsubscribe/index.js
T
Jaco Greeff 9dd0c6cfd8 Update examples for 0.90 (#1225)
* Update examples for 0.90

* Fix number typo

* Adapth to use timestamp.now (blockPeriod now in all versions)
2019-07-29 20:19:52 +02:00

27 lines
757 B
JavaScript

/* eslint-disable @typescript-eslint/no-var-requires */
// Import the API
const { ApiRx } = require('@polkadot/api');
const { switchMap } = require('rxjs/operators');
async function main () {
// Create a new instance of the api
// Subscribe to chain updates and log the current block number on update.
const subscription = new ApiRx().isReady
.pipe(
switchMap((api) =>
api.rpc.chain.subscribeNewHead()
))
.subscribe((header) => {
console.log(`Chain is at block: #${header.number}`);
});
// In this example we're calling the Overvables unsubscribe() //
// function after 20s.
setTimeout(() => {
subscription.unsubscribe();
console.log('Unsubscribed');
}, 20000);
}
main().catch(console.error);