initial commmit

This commit is contained in:
schrom01
2022-10-27 08:21:03 +02:00
commit 2a21c4136e
16 changed files with 97311 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
//
// async and await
//
function resolveAfter2Seconds(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve(x)
}, 2000)
})
}
// with async and await
async function add (x) {
let a = resolveAfter2Seconds(20)
let b = resolveAfter2Seconds(30)
return x + await a + await b
}
// with Promise.all
function addP (x) {
return Promise.all([
resolveAfter2Seconds(20),
resolveAfter2Seconds(30)
]).then(([a, b]) => x + a + b)
}
add(10).then(console.log)
addP(10).then(console.log)
+17
View File
@@ -0,0 +1,17 @@
//
// Example using EventEmitter
//
const EventEmitter = require('events')
const door = new EventEmitter()
door.on('open', (arg) => {
console.log('Door was opened ' + arg)
})
process.nextTick(() => {
console.log('next tick')
door.emit('open', 'async')
})
door.emit('open', 'sync')
+23
View File
@@ -0,0 +1,23 @@
//
// timeout vs immediate
//
const fs = require('fs')
setTimeout(() => {
console.log('timeout')
}, 0)
setImmediate(() => {
console.log('immediate')
})
fs.readFile("immediate2.js", () => {
setTimeout(() => {
console.log('timeout from readFile callback')
}, 0)
setImmediate(() => {
console.log('immediate from readFile callback')
})
})
console.log('script started')
+30
View File
@@ -0,0 +1,30 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Event Demo</title>
<script>
window.onload = () => {
var bibimg = document.querySelector('img')
bibimg.src = "https://www.zhaw.ch/storage/_processed_/b/2/"
+ "csm_hsb-winterthur-aussenansicht-vorne_3198f01cca.jpg"
bibimg.addEventListener('load', function() {
alert("Image loaded")
})
var timestamp = Date.now() + 3000
while (Date.now() < timestamp) {}
}
</script>
</head>
<body>
<div>
<img src="" alt="Hochschulbibliothek in Winterthur">
</div>
</body>
</html>
+16
View File
@@ -0,0 +1,16 @@
//
// Naive asynchronous code. This doesnt work!
//
let fs = require('fs')
let timestamp = new Date().toString()
let contents
fs.writeFile('date.txt', timestamp, () => {})
fs.readFile('date.txt', (err, data) => {
if (err) throw err
contents = data
})
console.log('Comparing the contents')
console.assert(timestamp == contents)
+22
View File
@@ -0,0 +1,22 @@
//
// nexttick vs. immediate and timeout events
//
const fs = require('fs')
const process = require('process')
fs.readFile("nexttick.js", () => {
setTimeout(() => {
console.log('timeout')
}, 0)
setImmediate(() => {
console.log('immediate')
})
process.nextTick(() => {
console.log('nexttick')
})
})
+20
View File
@@ -0,0 +1,20 @@
//
// Rejecting a promise by throwing an error in the constructor
// callback or in the handler chain
//
var promise = new Promise((resolve, reject) => {
// reject()
// throw Error('fail')
resolve()
})
promise
.then(() => console.log('step1'))
// .then(() => { throw Error('fail') })
.then(() => console.log('step2'))
.catch (() => console.log('catch1'))
.then(() => console.log('step3'))
.catch (() => console.log('catch2'))
.then(() => console.log('step4'))
+26
View File
@@ -0,0 +1,26 @@
//
// Passing values in a sequence of steps
//
function step1() {
return Promise.resolve('ta-da!')
}
step1().then(
function step2(result) {
console.log('Step 2 received ' + result)
return 'Greetings from step 2'
})
.then(
function step3(result) {
console.log('Step 3 received ' + result)
})
.then(
function step4(result) {
console.log('Step 4 received ' + result)
return Promise.resolve('Use my fulfilled value')
})
.then(
function step5(result) {
console.log('Step 5 received ' + result)
})
+60
View File
@@ -0,0 +1,60 @@
//
// One promise with multiple consumers
//
// The getProfile method creates a promise that is resolved with
// a mock profile. The user is then passed to navbar.showUsername()
// and account.showProfile().
//
// Remember that a promise serves as a placeholder for the result
// of an operation. Here, the user.profilePromise is a placeholder
// used by the showUsername and show Profile functions.
// These functions can be safely called at anytime before or after
// the profile data is available. The callbacks they use to print
// the data to the console will only be invoked once the profile
// is loaded.
//
var user = {
profilePromise: null,
getProfile: function () {
if (!this.profilePromise) {
// This code would request the profile from the server
var mockProfile = {
name: 'Samantha',
subscribedToSpam: false
}
this.profilePromise = new Promise(function (resolve, reject) {
setTimeout(()=>resolve(mockProfile), 1000)
})
}
return this.profilePromise
}
}
var navbar = {
showUsername: function (user) {
user.getProfile().then(function (profile) {
console.log('*** Navbar ***')
console.log('Name: ' + profile.name)
})
}
}
var account = {
showProfile: function (user) {
user.getProfile().then(function (profile) {
console.log('*** User Profile ***')
console.log('Name: ' + profile.name)
console.log('Send lots of email? ' + profile.subscribedToSpam)
})
}
}
navbar.showUsername(user)
account.showProfile(user)
+28
View File
@@ -0,0 +1,28 @@
//
// Priorities in the event loop
//
Promise.resolve().then(() => console.log('promise resolved'))
setImmediate(() => console.log('set immediate'))
process.nextTick(() => console.log('next tick'))
setTimeout(() => console.log('set timeout'), 0)
Promise.resolve().then(() => console.log('promise1 resolved'))
Promise.resolve().then(() => console.log('promise2 resolved'))
Promise.resolve().then(() => {
console.log('promise3 resolved')
process.nextTick(() => console.log('next tick inside promise resolve handler'))
})
Promise.resolve().then(() => console.log('promise4 resolved'))
Promise.resolve().then(() => console.log('promise5 resolved'))
setImmediate(() => console.log('set immediate1'))
setImmediate(() => console.log('set immediate2'))
process.nextTick(() => console.log('next tick1'))
process.nextTick(() => console.log('next tick2'))
process.nextTick(() => console.log('next tick3'))
setTimeout(() => console.log('set timeout'), 0)
setImmediate(() => console.log('set immediate3'))
setImmediate(() => console.log('set immediate4'))
+22
View File
@@ -0,0 +1,22 @@
//
// The Promise.all() function accepts an array of promise
// instances and returns a new promise which is fulfilled
// once all the promises in the array are fulfilled or
// rejected as soon as any of the promises in the array
// are rejected.
//
// The Promise.race() function accepts an array of promise
// instances and is fulfilled or rejected as soon as one of
// the promises in the array is fulfilled or rejected.
//
var p1 = new Promise((resolve, reject) => {
setTimeout(resolve, 2000, 'first')
})
var p2 = new Promise((resolve, reject) => {
setTimeout(resolve, 3000, 'second')
})
Promise.all([p1, p2]).then(console.log)
Promise.race([p1, p2]).then(console.log)
+11
View File
@@ -0,0 +1,11 @@
//
// wait returns a promise that is resolved after some time
//
function wait (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
wait(2000).then(() => console.log("first"))
wait(2000).then(() => console.log("second"))
+11
View File
@@ -0,0 +1,11 @@
Quelle für diese Datei:
https://ourworldindata.org/world-population-growth#population-growth-by-country
HINWEIS:
Die Originalversion der CSV-Datei hat in der ersten Zeile einen String, welcher
selbst Kommas enthält. Das macht das Lasen unnötig kompliziert, daher ist die
Originalversion nun unter population_orig.csv abgelegt und eine vereinfachte
Version ist unter population.csv zu finden.
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff