Created
July 28, 2014 08:00
-
-
Save vnys/372473fe38d96c3f06ce to your computer and use it in GitHub Desktop.
promises experiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Promise = require('es6-promise').Promise; | |
function get(url) { | |
return new Promise(function(resolve, reject) { | |
var req = new XMLHttpRequest(); | |
req.open('GET', url); | |
req.onload = function() { | |
if (req.status === 200) { | |
resolve(req.response); | |
} else { | |
reject(Error(req.statusText)); | |
} | |
} | |
req.onerror = function() { | |
reject(Error('Network error')) | |
} | |
}) | |
} | |
get('file.json').then(function(){}, function(){}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment