Created
July 3, 2017 07:33
-
-
Save tmpaul06/76562a0f2a6e2ba975b470da08782db0 to your computer and use it in GitHub Desktop.
Promise constructor
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
function getSomeJSON(url, callback) { | |
// AJAX call to get data from server. Accepts callback and calls | |
// with callback(err, response) | |
... | |
} | |
function fetchJSON(url) { | |
return new Promise(function (resolve, reject) { | |
getSomeJSON(url, function (err, response) { | |
if (err) return reject(err); | |
resolve(response); | |
}); | |
}); | |
} | |
fetchJSON("http://www.google.com").then(..); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment