I'm new to Datapower Gateway script (and Javascript) and following situation totally confuses me. Look:
var inputJson = "default";
//Reading json from input and covert it to string
session.input.readAsJSON( function ( error, json) {
if ( error ) {
session.reject( 'Input is not a valid JSON document' );
return;
}
inputJson = JSON.stringify(json);
console.debug("Inside: ", inputJson);
});
console.debug("Outside ", inputJson);
In Datapower console will be following:
"Inside: { long json string }"
"Outside: default"
It totally breaks my mind and distort my knowleges about variables scopes. Is it feature of javascript, datapower script implementation or what?
UPD. And another brain-cracked thing:
function getFile(options){
var file="default";
urlopen.open(options,function(error, response){
if(error){
console.error("Unable to find file: "+JSON.stringify(error));
}else{
if(response.statusCode==200){
response.readAsBuffer(function(error, responseData){
if(error){
console.error("Unable to open file: "+JSON.stringify(error));
}else{
console.error("Before: ", file);
file=responseData.toString('ascii');
console.error("After: ", file);
}
});
}else{
console.error("Unable to open file: "+response.statusCode);
}
}
});
return file;
}
console.error("Func result: ", getFile(openSchemaOptions));
Console result:
"Func result: default" (sic!)
"Before: default"
"After: --json string--"
How it could be possible to print function result before the function execution?!