Skip to content

Instantly share code, notes, and snippets.

@shlomitc
shlomitc / Fetch - POST - JSON
Created June 21, 2016 14:09
How to use the fetch api to send a json object
fetch('http://localhost:4000/generate',
{
method: "POST",
headers: {
"Content-type": "application/json; charset=UTF-8"
},
body: JSON.stringify({a:1})
})
function createMe(config){
return {
callMeString: function(){
console.log('who: ' + config);
},
callMeObject: function(){
console.log('who: ' + (config && config.who));
}
};
}
@shlomitc
shlomitc / reject-resolve
Last active August 29, 2015 14:20
rejected and resolved promises from jQuery
var getRejectedPromise = function(data){
if (typeof data !== 'undefined') {
data = [].concat(data);
}
var deferred = new jQuery.Deferred();
deferred.reject.apply(deferred, data);
return deferred.promise();
};
var getResolvedPromise = function(data){
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
<style id="jsbin-css">
body {
margin: 0;
padding: 0;
}