Skip to content

Instantly share code, notes, and snippets.

View abegehr's full-sized avatar
👨‍💻

Anton Begehr abegehr

👨‍💻
View GitHub Profile
@abegehr
abegehr / sleepjs1_promise_capabilities.js
Last active May 13, 2020 10:47
Sleepjs has all standard JS Promise capabilities
// https://github.com/simsieg/sleepjs
const { sleep, sleepSeconds, sleepMinutes } = require('sleepjs')
// chain sleeps of different intervals
async function chainingFunc() {
await sleep(500);
alert("Hello");
await sleepSeconds(1);
alert("Hello");
await sleepSeconds(2);
@abegehr
abegehr / sleepjs1_async_sleep_and_alert.js
Last active May 13, 2020 10:29
sleepjs async sleep and alert
// https://github.com/simsieg/sleepjs
const { sleep } = require('sleepjs')
async function myFunction() => {
await sleep(500);
alert("Hello");
}
myFunction();
@abegehr
abegehr / sleepjs1_setTimeout.js
Last active May 13, 2020 10:29
setTimeout Function
// https://www.w3schools.com/jsref/met_win_settimeout.asp
function myFunction() {
setTimeout(function() {
alert("Hello");
}, 3000);
}
myFunction();
@abegehr
abegehr / jspromise2_option2.js
Last active May 7, 2020 10:32
Fetching data from Firestore simultaneously.
// https://firebase.google.com/docs/firestore/quickstart
var db = firebase.firestore();
async function fetchArticle(articleId, authorId) {
// get author and article docs simultaneously
const authorPromise = db.doc("authors/" + authorId).get();
const articlePromise = db.doc("articles/" + articleId).get();
// wait until both compleeted
@abegehr
abegehr / jspromise2_option1.js
Created May 6, 2020 10:30
Fetching data from Firestore sequentially.
// https://firebase.google.com/docs/firestore/quickstart
var db = firebase.firestore();
async function fetchArticle(articleId, authorId) {
try {
// get author and article docs sequentially
const authorSnap = await db.doc("authors/" + authorId).get();
const articleSnap = await db.doc("articles/" + articleId).get();
}
catch (err) {/* handle error */}
@abegehr
abegehr / jspromise1_async, await, try, catch.js
Last active May 6, 2020 10:29
Promise async, await, try, catch Example
// https://firebase.google.com/docs/firestore/quickstart
async function main() {
try {
const doc = await db.doc("user/" + uid).get()
if (doc.exists) {
console.log("User data:", doc.data());
// use fetched data, for example in setState()
} else {
@abegehr
abegehr / jspromise1_Promise.then().catch().js
Created April 30, 2020 10:32
Promise.then().catch(); Example
// https://firebase.google.com/docs/firestore/quickstart
const userPromise = db.doc("user/" + uid).get().then(doc => {
if (doc.exists) {
console.log("User data:", doc.data());
// use fetched data, for example in setState()
} else {
console.warn("No user data.");
}
}).catch(error => {
// https://firebase.google.com/docs/firestore/quickstart
var db = firebase.firestore();
const userPromise = db.doc("user/" + uid).get();
const articlePromise = db.doc("article/1").get();
@abegehr
abegehr / Flutter_SensorsTest.dart
Created April 28, 2020 09:04
Testing Flutter sensors (acc, userAcc, gyro)
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:sensors/sensors.dart';
class SensorsTest extends StatefulWidget {
SensorsTest({Key key}) : super(key: key);
@override
_SensorsTestState createState() => _SensorsTestState();
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.