Asynchronous means that things occur when they are ready (without any synchronous relationship to each other.)
This implies that if you wish to print or otherwise operate on a value created within an asynchronous function, you MUST do so in its callback since that is the only place that the value is guaranteed to be available.
If you wish to order multiple activities so they occur one after another, you'll need to apply one of the various techniques which "chain" asynchronous functions in a synchronous way (one after another) such as the async javascript library written by caolan.
Using async in your example, you would:
var async=require('async');
var toObtain;
async.series([
function(next){ // step one - call the function that sets toObtain
im.identify('kittens.png', function(err, features){
if (err) throw err;
toObtain = features;
next(); // invoke the callback provided by async
});
},
function(next){ // step two - display it
console.log('the value of toObtain is: %s',toObtain.toString());
}
]);
The reason this works is because the async library provides a special callback that is used to move to the next function in the series which must be called when the current action has completed.
See the async documentation for further info, including how to pass values through the next() callback to the next function in the series and as the result of the async.series() function.
async can be installed to your application using:
npm install async
or globally, so it is available to all your nodejs applications, using:
npm install -g async