Files
api/docs/examples/promise/04_unsubscribe/index.js
T
Jaco Greeff c4ba8ca64d Add ts-check for examples (#705)
* Add ts-check for examples

* Typing cleanups, now shows properly

* Bump

* Cleanup checks

* Bump keyring
2019-02-16 12:25:53 +01:00

24 lines
649 B
JavaScript

// @ts-check
// Import the API
const { ApiPromise } = require('@polkadot/api');
async function main () {
// Create a new instance of the api
const api = await ApiPromise.create();
// Subscribe to chain updates and log the current block number on update.
const unsubscribe = await api.rpc.chain.subscribeNewHead((header) => {
console.log(`Chain is at block: #${header.blockNumber}`);
});
// In this example we're calling the unsubscribe() function that is being
// returned by the api call function after 20s.
setTimeout(() => {
unsubscribe();
console.log('Unsubscribed')
}, 20000);
}
main().catch(console.error);