Discussion:
can log but unable to return value from addCallback
hotani
2010-11-15 18:04:24 UTC
Permalink
I can log the value, but it returns as "undefined".

var getOffice = function(county_id) {
var d = loadJSONDoc('/office_from_countytype/'+ county_id +'/');
var off_id;
d.addCallback(function (result) {
off_id=result[0].fields['office'];
log('got office id: '+ off_id);
return off_id;
});
}

The log entry will show "INFO: got office id: 14", but the function
returns "undefined".
--
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to mochikit+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en.
Fredrik
2010-11-15 21:45:03 UTC
Permalink
You're confusing the return inside addCallback with the return from
the outer getOffice function.

--- modified example:
var getOffice = function(county_id) {
var d = loadJSONDoc('/office_from_countytype/'+ county_id +'/');
var off_id;
d.addCallback(function (result) {
off_id=result[0].fields['office'];
log('got office id: '+ off_id);
return off_id;
});
return d; // <--- !
}

... now, a user can get 'off_id' like this.

var d = getOffice(country_id);
d.addCallback(function(off_id) {
log('got office id:', off_id);
});

(since getOffice makes an asynchronous request any return from it
must, by definition, also be asynchronous.)

HTH
// Fredrik Blomqvist
Post by hotani
I can log the value, but it returns as "undefined".
var getOffice = function(county_id) {
    var d = loadJSONDoc('/office_from_countytype/'+ county_id +'/');
    var off_id;
    d.addCallback(function (result) {
        off_id=result[0].fields['office'];
        log('got office id: '+ off_id);
        return off_id;
    });
}
The log entry will show "INFO: got office id: 14", but the function
returns "undefined".
--
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to mochikit+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en.
hotani
2010-11-15 23:34:21 UTC
Permalink
thanks for the response - I'll give this a try. I'm still trying to
wrap my head around the concepts of deferred and callbacks....
Post by Fredrik
You're confusing the return inside addCallback with the return from
the outer getOffice function.
var getOffice = function(county_id) {
    var d = loadJSONDoc('/office_from_countytype/'+ county_id +'/');
    var off_id;
    d.addCallback(function (result) {
        off_id=result[0].fields['office'];
        log('got office id: '+ off_id);
        return off_id;
    });
   return d; // <--- !
}
... now, a user can get 'off_id' like this.
var d = getOffice(country_id);
d.addCallback(function(off_id) {
    log('got office id:', off_id);
});
(since getOffice makes an asynchronous request any return from it
must, by definition, also be asynchronous.)
HTH
// Fredrik Blomqvist
Post by hotani
I can log the value, but it returns as "undefined".
var getOffice = function(county_id) {
    var d = loadJSONDoc('/office_from_countytype/'+ county_id +'/');
    var off_id;
    d.addCallback(function (result) {
        off_id=result[0].fields['office'];
        log('got office id: '+ off_id);
        return off_id;
    });
}
The log entry will show "INFO: got office id: 14", but the function
returns "undefined".
--
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to mochikit+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en.
Loading...