Skip to content

Instantly share code, notes, and snippets.

@jhines2k7
jhines2k7 / resolve-multiple-values
Created July 7, 2015 18:06
Resolving multiple values from a JavaScript function
function someAsyncFunction(data) {
return new Promise(function(resolve, reject) {
resolve([console.log(data), console.log('Resolved in someAsyncFunction!')]);
});
}
var fetchData = function() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve({
@jhines2k7
jhines2k7 / create-a-simple-javascript-promise
Created July 7, 2015 18:11
Creating a simple JavaScript Promise
var promise = new Promise(function(resolve, reject) {
// do a thing, possibly async, then…
if (/* everything turned out fine */) {
resolve("Stuff worked!");
}
else {
reject(Error("It broke"));
}
});
@jhines2k7
jhines2k7 / use-a-simple-javascript-promise
Created July 7, 2015 18:12
Using a simple JavaScript Promise
promise.then(function(result) {
console.log(result); // "Stuff worked!"
}, function(err) {
console.log(err); // Error: "It broke"
});
@jhines2k7
jhines2k7 / reject-value-error-callback
Created July 7, 2015 18:31
Rejecting a value and handling it in the error callback
function someAsyncFunction(data) {
return new Promise(function(resolve, reject) {
resolve([console.log(data), console.log('Resolved in someAsyncFunction!')]);
})
}
var fetchData = function() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
/*resolve({
@jhines2k7
jhines2k7 / resolving-promises-promise-all.js
Last active August 29, 2015 14:24
Resolving an array of promises using Promise.all
function firstAsyncFunction() {
return new Promise(function(resolve, reject){
setTimeout(function(){
resolve('First async function has been resolved!');
}, 1000);
});
}
function secondAsyncFunction() {
return new Promise(function(resolve, reject) {
@jhines2k7
jhines2k7 / gulp-compass
Created July 8, 2015 02:45
Simple gulp task for using Compass
var gulp = require('gulp'),
compass = require('gulp-compass');
gulp.task('compass', function() {
return gulp.src('styles/sass/*.scss')
.pipe(compass({
config_file: './config.rb',
css: './styles/css',
sass: './styles/sass'
}))
@jhines2k7
jhines2k7 / functional-javascript-compose-function
Created July 8, 2015 18:33
A function used to build complex functions from many simple, generic functions
Function.prototype.compose = function(prevFunc) {
var nextFunction = this;
return function() {
return nextFunction.call(this, prevFunc.apply(this, arguments));
}
};
@jhines2k7
jhines2k7 / iife-jshint
Created July 15, 2015 22:31
Test IIFE JSHint errors
var Hello = (function(){
'use strict';
var _name;
function Hello(name){
_name = name;
}
Hello.prototype.sayHello = function(){
@jhines2k7
jhines2k7 / iife-jshint-2
Created July 16, 2015 01:30
IIFE that passes JSHint and runs correctly in a Jasmine test
var Hello = (function(){
'use strict';
var _name;
function Hello(name){
_name = name;
}
Hello.prototype.sayHello = function(){
@jhines2k7
jhines2k7 / sticky-tail-headers-different-heights.js
Last active August 29, 2015 14:25
Sticky tail headers with different heights
var tailHeaders = $('.tail-header');
for(var i = 0; i < tailHeaders.length; i++) {
new Waypoint({
element: tailHeaders[i],
context: $('#container'),
handler: function(direction) {
var prevTailHeader = this.previous() ? this.previous().adapter.$element : null;
var nextTailHeader = this.next() ? this.next().adapter.$element : null;