Thursday 22 December 2016

How to keep a proper reference to "this" in callbacks?

If I want to wrap request so that encoding will be kept in mind...function requestWithEncoding(chosenURL,callback) { request({url: chosenURL, encoding: null},function(err,response,body) { if(err) return callback(err,response,body); else { var properEncoding = getEncoding(response); response.body = iconv.decode(response.body, properEncoding); body = iconv.decode(response.body, properEncoding); return callback(null,response,body); } }); } But then do something like "this.uri.href" in the callback, the script won't know what I'm talking about

Submitted December 22, 2016 at 03:31PM by DoomTay

No comments:

Post a Comment