Skip to content

Instantly share code, notes, and snippets.

View mihansweatpants's full-sized avatar

Mikhail Barsegyan mihansweatpants

View GitHub Profile

Развернуть многомерный массив

flat([1, 2, [3, 4]]); // [1, 2, 3, 4]
flat([1, 2, [3, [4]]]); // [1, 2, 3, 4]

Вложенность не ограничена.

Решение

Run multiple tasks from one script and kill them all on ctrl+c

Took my dumbass two hours to figure this out

#!/bin/bash

trap "exit" INT TERM ERR
trap "kill -s KILL 0" EXIT

Resolve promises in sequence

const task1 = () => fetch('/api/resource1').then(res => res.json());
const task2 = () => fetch('/api/resource2').then(res => res.json());
const task3 = () => fetch('/api/resource3').then(res => res.json());

const tasks = [task1, task2, task3];

tasks

Conditionally add a key-value pair to an object

const someObj = {
  ...(someCondition && { someProp: 5 })
}

Destructuring let assignment

// Saw this inside reacts render method but mb this can be used somewhere else
render() {
  const { somethingAsync: { result } } = this.props;
  let total: null | number = null;

  // Grab something from result when it is available
 if (result != null) {

Go-like error handling

Stole from here 🙂

/* Helper for removing async/await try/catch stuff */
function O_o(promise) {
  return promise.then((data) => {
    if (data instanceof Error) return [data];
    return [null, data];
 }).catch(err => [err]);
@mihansweatpants
mihansweatpants / AsyncComponent.jsx
Created August 5, 2018 08:53 — forked from lencioni/AsyncComponent.jsx
<AsyncComponent> at Airbnb used for Webpack code splitting
// Usage:
//
// function loader() {
// return new Promise((resolve) => {
// if (process.env.LAZY_LOAD) {
// require.ensure([], (require) => {
// resolve(require('./SomeComponent').default);
// });
// }
// });