Last active
February 22, 2017 04:57
-
-
Save lassombra/38d9fb63eb8b973eda3b78612e758282 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function get(url) { | |
return new Promise((resolve, reject) => { | |
$.get(url, {success: resolve, error: reject}) | |
}); | |
} |
This file contains hidden or 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
request.get('url', (error, result) => { | |
if (error) { | |
console.error(error); | |
} else { | |
request.get(result, (error, result) => { | |
if (error) { | |
console.error(error); | |
} else { | |
console.log(result); | |
}); | |
} | |
}); |
This file contains hidden or 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
function requestWithPromise(method, url) { | |
return new Promise((resolve, reject) => { | |
request.call(method, url, (error, result) => { | |
if (error) { | |
reject(error); | |
} else { | |
resolve(result); | |
} | |
}); | |
}); | |
} |
This file contains hidden or 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
// fetch is a newer api that does http requests and returns a promise for them | |
fetch('someUrl') | |
.then(result => fetch(result)) | |
.then(result => console.log(result)) | |
.catch(error => console.error(error)) |
This file contains hidden or 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
function wrapAsync(asyncFunc) { | |
return function wrappedFunc(...args) { | |
return new Promise((resolve, reject) => { | |
asyncFunc(...args, (error, result) => { | |
if (error) { | |
reject(error); | |
} else { | |
resolve(result); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment