Published
Edited
Feb 7, 2018
3 stars
Insert cell
md`# async / await`
Insert cell
Insert cell
Insert cell
function sleep(t) {
return new Promise(r => setTimeout(() => r(Math.random()), t));
}
Insert cell
Insert cell
Insert cell
// cool
async function waitthen(){
await sleep(1000);
return Math.random();
}
Insert cell
Insert cell
Insert cell
Insert cell
d3Fetch = require("d3-fetch@1")
Insert cell
Insert cell
// This will fail
require('d3').geoMercator()([0,0])
Insert cell
Insert cell
// With promises, this will work!
require('d3').then(d3 => d3.geoMercator()([0, 0]))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
let countries = (await d3Fetch.tsv("https://unpkg.com/world-atlas@1/world/110m.tsv"))
.sort((a, b) => b.pop_est - a.pop_est) // Sort by descending estimated population.
.slice(0, 10) // Take the top ten.
return countries;
}
Insert cell
Insert cell
status = new Promise(resolve => {
setTimeout(() => {
resolve({resolved: new Date});
}, 2000);
})
Insert cell
Insert cell
status
Insert cell
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