Public
Edited
Jan 25, 2023
14 forks
Importers
189 stars
Also listed in…
JavaScript in Observable
Insert cell
Insert cell
Insert cell
Insert cell
yield 42
Insert cell
Insert cell
42
Insert cell
Insert cell
{
let i = 0;
while (true) {
yield Promises.delay(1000, ++i);
}
}
Insert cell
Insert cell
{
let i = 0;
while (true) {
await Promises.delay(1000);
yield ++i;
}
}
Insert cell
Insert cell
tick = {
while (true) {
yield Promises.delay(1000);
}
}
Insert cell
tick, md`This number updates once per second: \`${Math.random()}\``
Insert cell
Insert cell
{
const div = html`<div style="transition:background 250ms linear;height:33px;">`;
yield div;
while (true) {
div.style.background = "red";
yield Promises.delay(1000, div);
div.style.background = "green";
yield Promises.delay(1000, div);
div.style.background = "blue";
yield Promises.delay(1000, div);
}
}
Insert cell
Insert cell
minutes = {
let i;
yield i = 0;
while (true) yield Promises.delay(60000, ++i);
}
Insert cell
minutes // Shift-Enter or hit play to rerun this cell.
Insert cell
Insert cell
{
let i = 0;
while (true) {
yield ++i;
}
}
Insert cell
Insert cell
{
yield 1;
yield 2;
yield 3;
return "ignored"; // It’s ignored!
}
Insert cell
Insert cell
yield* [1, 2, 3]
Insert cell
yield* new Set([1, 2, 3])
Insert cell
Insert cell
{
while (true) {
yield* range(0, 100);
}
}
Insert cell
Insert cell
input = html`<input type=range>`
Insert cell
Generators.observe(next => {
// Yield the input’s initial value.
next(input.value);

// Define an event listener to yield the input’s next value.
const inputted = () => next(input.value);

// Attach the event listener.
input.addEventListener("input", inputted);

// When the generator is disposed, detach the event listener.
return () => input.removeEventListener("input", inputted);
})
Insert cell
Insert cell
Generators.input(input)
Insert cell
Insert cell
viewof value = html`<input type=range>`
Insert cell
value
Insert cell
Insert cell
{
try {
for (let i = 0; i < 100; ++i) {
yield i;
}
} finally {
console.log("terminated!");
}
}
Insert cell
Insert cell
function* range(start, stop) {
start = Math.floor(start);
stop = Math.ceil(stop);
while (start < stop) yield start++;
}
Insert cell
Insert cell
range(0, 100)
Insert cell
Insert cell
width
Insert cell
Insert cell
now
Insert cell
Insert cell
Math.sin(now / 1000)
Insert cell
Insert cell
height = 300
Insert cell
import {canvas} with {height} from "@mbostock/connected-particles-iii"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more