Skip to content

Instantly share code, notes, and snippets.

@ryansutc
Last active April 22, 2019 15:32
Show Gist options
  • Save ryansutc/0d8f9e97d9b59f675a5c328dba1abf13 to your computer and use it in GitHub Desktop.
Save ryansutc/0d8f9e97d9b59f675a5c328dba1abf13 to your computer and use it in GitHub Desktop.
then-able javascript function example
/*
* An example of a function fetching data
* and returning a promise, making itself
* "thenable".
ref: https://scotch.io/@wesleylhandy/writing-your-own-javascript-promises
*/
function fetchIds(url) {
return new Promise((resolve, reject) => {
queryUrl = "/query?f=json&where=1%3D1&returnIdsOnly=true"
fetch(url + queryUrl)
.then(function (response) {
return response.json();
})
.then(function (data) {
//return new Promise(function (resolve, reject) {
resolve(data.objectIds)
})
.catch(function (err) {
console.log(err);
console.log("something went south");
reject(err);
})
});
}
function countRecords() {
var url = "https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Major_Cities/FeatureServer/0"
fetchIds(url).then(function (ids) {
console.log(ids.length)
})
}
@ryansutc
Copy link
Author

ryansutc commented Mar 6, 2019

Everytime I come back to javascript after being away promises throw me. Vanilla IE safe javascript example above for future reference.

@ryansutc
Copy link
Author

ryansutc commented Apr 7, 2019

Actually fetch() might need ES5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment