Last active
          April 22, 2019 15:32 
        
      - 
      
- 
        Save ryansutc/0d8f9e97d9b59f675a5c328dba1abf13 to your computer and use it in GitHub Desktop. 
    then-able javascript function example
  
        
  
    
      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
    
  
  
    
  | /* | |
| * 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) | |
| }) | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Actually fetch() might need ES5