Skip to content

Instantly share code, notes, and snippets.

View SegersIan's full-sized avatar

Segers Ian SegersIan

View GitHub Profile
function fetchData(){
return Promise.resolve({ name : 'John' })
}
async function fetchAndLogData () {
const data = await fetchData();
console.log(data);
return;
@SegersIan
SegersIan / comparison.js
Last active July 26, 2017 14:42
literal objects(literals) vs constructor functions vs factory functions
// Factory function/method
function factoryFunction (){
return {
field : 'value',
method : function() { return 5; }
}
}
const myNewObjectA = factoryFunction();
// Constructor Function
@SegersIan
SegersIan / 1.js
Created August 17, 2017 15:51 — forked from getify/1.js
comparing single recursion vs binary recursion
function reverseStr(str) {
if (str.length <= 1) return str;
var firstChar = str[0];
var restStr = str.substr(1);
return (restStr.length > 1 ? reverseStr(restStr) : restStr) +
firstChar;
}
@SegersIan
SegersIan / baseConverter.js
Created September 7, 2017 18:39 — forked from faisalman/baseConverter.js
Convert From/To Binary/Decimal/Hexadecimal in JavaScript
/**
* Convert From/To Binary/Decimal/Hexadecimal in JavaScript
* https://gist.github.com/faisalman
*
* Copyright 2012-2015, Faisalman <fyzlman@gmail.com>
* Licensed under The MIT License
* http://www.opensource.org/licenses/mit-license
*/
(function(){
@SegersIan
SegersIan / Store.js
Created September 14, 2017 15:41
How to handle updates on a single item in an array located in the Vuex store.
import vuex from 'vuex';
import vue from 'vue';
import * as api from './api';
vue.use(vuex);
export default new vuex.Store({
state: {
users: []
<template>
<div> {{userProfile.name}} </div>
</template>
<script>
export default {
// here goes ... name, computed, mapping my state etc...
beforeMount(){
this.$store.dispatch('fetchUserProfile');
}
@SegersIan
SegersIan / async-generator.js
Last active April 27, 2018 12:46
Code example of using an async generator in JS
/* Imports */
const request = require('request-promise-native')
const assert = require('assert')
/* My API call*/
async function getBooks({query, startIndex, maxResults}) {
const response = await request({
method: 'GET',
url: 'https://www.googleapis.com/books/v1/volumes',
json: true,
const request = require('request-promise-native')
async function getBooks({query, startIndex, maxResults}) {
const response = await request({
method: 'GET',
url: 'https://www.googleapis.com/books/v1/volumes',
json: true,
qs: {
q: query,
startIndex: startIndex,
async function* myFunc(){
// Do something
}
async function* searchBooks({query, pages}) {
for (let currentPage = 0; currentPage < pages; currentPage++) {
const books = await getBooks({
query : query,
startIndex: currentPage,
maxResults: 2
})
yield books
}
}