jquery - JavaScript static function in callback -
hi i've been trying clarify there's i'm still confused about. know can't return values asynchronous functions i've referenced answer's top answer returning value asynchronous javascript method?
what i'm trying use flickrapi biggest size image. flickrapi allows 1 search images, use photo_id, use photo_id procses request api's getsize method url biggest size photo.
the code looks little messy is, because have method called flickrrequest sends xmlhttp request , gets json string. know can achieve want writing functions follows:
function flickrqforimage() { ...got id function flickrrqforsize() { ...got maxsizeurl create image based on maxsizeurl here } }
but wondering if possible this
function flickrqforimage() { ...got id function flickrrqforsize() { ...got maxsizeurl } create image based on maxsizeurl here } or create image based on maxsizeurl here
in general question whether possible have callback function references statically defined function (i think?). specifics of function takes callback , id , url processing happens in callbacks:
flickrrq(options, cb)
i wondering whether/what happen if unnamed function instead else, flickrrq(options, processphoto(data)), , define function in separate method. makes sense me because want keep functionality url processing separate in attempt make code cleaner , more readable.
i tried following below , didn't work. nothing prints. have console.log in processphoto method. in fact inside of flickrrqforsize method seems not evaluate
flickrrqforsize(options, function(data) { processphoto(data) }
even though in flickrrqforsize definition, callback function taken argument. i'm suspecting there must functions/async calls don't understand.
i hope clear -- if not, can post actual code.
here's code:
var flickrrequest = function(options, xhrrq, cb) { var url, xhr, item, first; url = "https://api.flickr.com/services/rest/"; first = true; (item in options) { if (options.hasownproperty(item)) { url += (first ? "?" : "&") + item + "=" + options[item]; //parses search equest; first = false; } } //xmlhttprq flickr if(xhrrq == 1 ) { xhr = new xmlhttprequest(); xhr.onload = function() { cb(this.response); }; xhr.open('get', url, true); xhr.send(); }; } var processphotosize = function(photojson) { var parsedjson = json.parse(data); var last = parsedjson.sizes.size.length; console.log(parsedjson.sizes.size[last-1].source); return parsedjson.sizes.size[last-1].source; } ... flickrrequest(options, 1, function(data) { ... flickrrequest(sizesoptions, 0, function(data) { parsedjson = json.parse(data); console.log(parsedjson); processphotosize(data); }); }
Comments
Post a Comment