Skip to content

Instantly share code, notes, and snippets.

@adregan
Last active November 23, 2015 22:42
Show Gist options
  • Save adregan/6872ff922910ea1fb9a5 to your computer and use it in GitHub Desktop.
Save adregan/6872ff922910ea1fb9a5 to your computer and use it in GitHub Desktop.
var requests = require('superagent');
var fetch = function(url, options) {
let method = options.method;
return new Promise((resolve, reject) => {
requests[method](url)
.end((err, res) => {
if (err) {
reject(err);
}
else {
resolve(res);
}
})
})
}
fetch('http://localhost:8888', {method: 'patch'})
.then(res => console.log(`SUCCESS: {JSON.stringify(res.body)}`))
.catch(err => {
if (err.crossDomain) {
console.error('Cross domain error!!!')
fetch('http://localhost:8888', {method: 'put'})
.then(res => console.log(`SUCCESS: {JSON.stringify(res.body)}`))
.catch(err => {
console.error('¯\_(ツ)_/¯');
})
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment