简体   繁体   中英

How to handle asynchronous callbacks in Node.js module?

This is my first stab at attempting to put together a node module and I am still trying to wrap my head around how to structure the asynchronous callbacks. This is a case in point. Right now I am trying to use featureService.getCount() and getting nothing in response. Using breakpoints, I know featureService.getUniqueIds() is working.

Since a callback is in there, I am assuming the reason why I am not getting a length back is the callback in getCount has not responded yet. After looking at this for most of the afternoon and not really coming up with a very good solution other than a recursive loop checking for the value to be populated with a timeout, I am asking for advice how to better structure my code to accomplish the task at hand.

I have read a bit about promises. Is this an applicable instance or even a viable solution? I really have no clue how to implement promises, but it makes logical sense in such an instance.

Obviously I am lost here. Thank you for any help you can offer.

var Client = require('node-rest-client').Client;
var client = new Client();

var featureService = function(endpoint){

    var uniqueIds;
    var count;

    // get list of unique id's
    this.getUniqueIds = function(){
        if (!uniqueIds) {
            var options = {
                parameters: {
                    f: 'json',
                    where: "OBJECTID LIKE '%'",
                    returnIdsOnly: 'true'
                }
            };
            client.get(endpoint + '/query', options, function(data, res){
                var dataObject = JSON.parse(data);
                var uniqueIds = dataObject.objectIds;
                return uniqueIds;
            });
        } else {
            return uniqueIds;
        }
    };

    // get feature count
    this.getCount = function(){

        // get uniqueIds
        uniqueIds = this.getUniqueIds();

        // get length of uniqueIds
        count = uniqueIds.length;
    };

    // get list of unique attribute values in a single field for typeahead
    this.getTypeaheadJson = function(field){};

    // find features in a field with a specific value
    this.find = function(field, value){};
};

var endpoint = 'http://services.arcgis.com/SgB3dZDkkUxpEHxu/arcgis/rest/services/aw_accesses_20140712b/FeatureServer/1';
var afs = new featureService(endpoint);
console.log(afs.getCount());

exports.featureService = featureService;

Now, after working it over some more and using request as in the bluebird documentation (I could not get the above module to work), I have this working, but cannot figure out how to get the calculated value to work with, the number of iterations.

var Promise = require("bluebird"),
    request = Promise.promisifyAll(require("request"));

var FeatureService = function(){

    // get count from rest endpoint
    var getCount = function(){
        var optionsCount = {
            url: endpoint + '/query',
            qs: {
                f: 'json',
                where: "OBJECTID LIKE '%'",
                returnCountOnly: 'true'
            }
        };
        return request.getAsync(optionsCount)
            .get(1)
            .then(JSON.parse)
            .get('count');
    };

    // get max record count for each call to rest endpoint
    var getMaxRecordCount = function(){
        var optionsCount = {
            url: endpoint,
            qs: {
                f: 'json'
            }
        };
        return request.getAsync(optionsCount)
            .get(1)
            .then(JSON.parse)
            .get('maxRecordCount');
    };

    // divide the total record count by the number of records returned per query to get the number of query iterations
    this.getQueryIterations = function(){
        getCount().then(function(count){
            getMaxRecordCount().then(function(maxCount){
                return  Math.ceil(count/maxCount);
            });
        });
    };

};

// url to test against
var endpoint = 'http://services.arcgis.com/SgB3dZDkkUxpEHxu/arcgis/rest/services/aw_accesses_20140712b/FeatureServer/1';

// create new feature service object instance
afs = new FeatureService();

// This seems like it should work, but only returns undefined
console.log(afs.getQueryIterations());

// this throws an error telling me "TypeError: Cannot call method 'then' of undefined"
//afs.getQueryIterations().then(function(iterCount){
//    console.log(iterCount);
//});

Yes, use promises! They're a powerful tool , made for exactly this purpose, and with a decent library they're easy to use. In your case:

var Promise = require('bluebird'); // for example, the Bluebird libary
var Client = Promise.promisifyAll(require('node-rest-client').Client);
var client = new Client();

function FeatureService(endpoint) {

    var uniqueIds;
    var count;

    // get list of unique id's
    this.getUniqueIds = function(){
        if (!uniqueIds) { // by caching the promise itself, you won't do multiple requests
                          // even if the method is called again before the first returns
            uniqueIds = client.getAsync(endpoint + '/query', {
                parameters: {
                    f: 'json',
                    where: "OBJECTID LIKE '%'",
                    returnIdsOnly: 'true'
                }
            })
            .then(JSON.parse)
            .get("objectIds");
        }
        return uniqueIds;
    };

    // get feature count
    this.getCount = function(){
        if (!count)
            count = this.getUniqueIds() // returns a promise now!
                    .get("length");
        return count; // return a promise for the length
    };

    // get list of unique attribute values in a single field for typeahead
    this.getTypeaheadJson = function(field){};

    // find features in a field with a specific value
    this.find = function(field, value){};
};

var endpoint = 'http://services.arcgis.com/SgB3dZDkkUxpEHxu/arcgis/rest/services/aw_accesses_20140712b/FeatureServer/1';
var afs = new FeatureService(endpoint);
afs.getCount().then(function(count) {
    console.log(count);
}); // you will need to use a callback to do something with async results (always!)

exports.FeatureService = FeatureService;

Here, using Bluebird's Promise.promisifyAll , you can just use .getAsync() instead of .get() , and will get a promise for the result.


 // divide the total record count by the number of records returned per query to get the number of query iterations this.getQueryIterations = function(){ getCount().then(function(count){ getMaxRecordCount().then(function(maxCount){ return Math.ceil(count/maxCount); }); }); }; 

That's the right idea! Only you always want to return something from .then handlers, so that the promise returned by the .then() call will resolve with that value.

// divide the total record count by the number of records returned per query to get the number of query iterations
this.getQueryIterations = function(){
    return getCount().then(function(count){
//  ^^^^^^ return the promise from the `getQueryIterations` method
        return getMaxRecordCount().then(function(maxCount){
//      ^^^^^^ return the promise for the iteration number
            return  Math.ceil(count/maxCount);
        });
    });
};

Now, you get back a promise for the innermost result , and this will work now:

afs.getQueryIterations().then(function(iterCount){
    console.log(iterCount);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM