Skip to content

Instantly share code, notes, and snippets.

View xjamundx's full-sized avatar

Jamund Ferguson xjamundx

View GitHub Profile
// basic error handling with async functions
async function getData(param) {
if (isBad(param)) {
throw new Error("this is a bad param")
}
// ...
}
// basic promise-based error handling example
function getData(param) {
// async function to show user data
async function displayUserData() {
let me = await fetch('/users/me')
console.log(me)
}
// promise-based equivalent
function displayUserData() {
return fetch('/users/me').then(function(me) {
console.log(me)
// here is an async function
async function getNumber() {
return 4 // actually returns a Promise
}
// the same function using promises
function getNumber() {
return Promise.resolve(4)
}
@xjamundx
xjamundx / svg.md
Created August 14, 2012 20:12
Everything I Know About SVG

Everything I Know About SVG

SVG is arguably going to be the main image format of the modern web. I recently wrote an article for Safari Books Online called SVG Icons for New Devices that covers some of the basics of dealing with SVG. This article covers more the pain points of using SVG in production and as it turns out there are many.

Plain Old <img> Tags

Here's how you link to your amazing vector image.

<img src="/images/logo.svg">
{ test: /modernizr.*/, loader: 'imports?this=>window!exports?window.Modernizr' },`
@xjamundx
xjamundx / oj3.service.js
Last active December 18, 2015 02:19
oj3.service.js
// oj3 implementation
angular.module('odesk').factory('oj3', function() {
return {
widgets: odesk.mediator.widgets,
trigger: function($scope, event, data) {
// i don't know, we aren't an instance
},
registerWidget: function(name, $scope) {
// this won't quite work, because $scope doesn't talk OJ3 events
oj3.registerWidget(name, $scope);
$.ajax({
url: 'https://www.odesk.com/',
success: function() {
$.ajax({
url: 'https://www.odesk.com/',
success: function() {
// so much nesting
}
});
});
@xjamundx
xjamundx / express-infinite.js
Created May 13, 2013 19:53
bug in express 3.x (including 3.2.4)
// this will cause an infinite loop
var express = require('express');
var app = express();
app.get('/', function(req, res) {
res.send(express.toast, "bla");
});
app.listen(2222);
@xjamundx
xjamundx / async.reporter.js
Created May 4, 2013 03:59
super simple example of how to do an async reporter for jshint (with my changes)
"use strict";
exports.reporter = function(errors, data, opts, cb) {
setTimeout(function() {
console.log(errors.length ? "FAILED" : "OK");
}, 50);
};
exports.reporter.async = true;
@xjamundx
xjamundx / views
Last active December 14, 2015 05:09 — forked from jquerygeek/views
define(function(require) {
var Backbone = require('backbone')
var template = require('text!../../template/newThread.html')
return Backbone.View.extend({
el: '#container',
template: _.template(template),
initialize: function () {
this.render()
var Contact = require('model/contact')