Our great sponsors
-
Since we will interact with the Hacker News API, let's clear the main.ts file and define the baseURL variable as follows:
-
Modern: Deno is built with modern technologies and follows a modern philosophy; it natively supports TypeScript and ES modules; it doesn't require a centralized package management solution; it also provides useful developer tools out of the box such as an opinionated source code formatter and a testing framework
-
InfluxDB
Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
-
In this post, we are going from zero to running a small but nontrivial script that fetches comments from Hacker News in real time using Deno, a new runtime for JavaScript and TypeScript created by Ryan Dahl, the original creator of Node.js.
-
// main.ts import { sleep } from "https://deno.land/x/sleep/mod.ts"; // ... async function streamComments() { let id = await getMaxItemID(); while (true) { const item = await getItemByID(id); if (!item) { await sleep(5); continue; } // Print only items that are visible comments const { type, deleted, dead } = item; const removed = deleted || dead || false; if (type === "comment" && !removed) { // Get the comment's author, if any const author = item.by ?? "unknown author"; // Get the comment's URL on HN website const hnURL = `https://news.ycombinator.com/item?id=${id}`; // Print the comment console.log(`\nRead new comment by ${author} on ${hnURL}`); console.log(item); } id += 1; } }
Related posts
- I have created a small anti-depression script
- Create a headless CMS using OceanBase and TypeScript: A step-by-step tutorial
- TypeScript Without Transpilation
- Node.js codebase is still entirely in Javascript and not in Typescript and node team does not even officially provide node TS types
- Creating a serverless API using AWS Lambda and Node.js with TypeScript and Express.js