Friday, 15 May 2015

javascript - Q promises - How does scope work? -


How does the radius of Q work? As far as I know that "then" callback is called from window, like setTimeout.

In this example (an example to understand how it works):

  var getFileText = function {var deferred = Q.defer (); Server.readFile ("foo.txt", "utf-8", function (error, text) {if (error) {deferred.reject (new error (error)); else {deferred.resolve (text);}} ); Refund refund. }; Var fu = function () {getFileText () Then (this.showFile); }; Foo.prototype.showFile = function (text) {this.text = text; Console.log (text); }; Var foo = new Foo ();   

I am using binds to keep text in foo example:

  var foo = function () {getFileText (). Then (this .showFile.bind (this)); };   

Is there any other way?

How does the scope of Q work?

You are looking for context .

I know the callback "then" is called from the window

Well, on on global context, yes. It has to be called from undefined to thisArg .

I am using the bind. Is there any other way?

In the context of the example only a long with the variable,

  var that = it; GetFileText () Then (work (text) {that.text = text; // or longer: // that.showFile (text);});    

No comments:

Post a Comment