Unbinds and its solution
When you are working with methods, instead of simple functions, it's highly likely that you can easily run into the common problems where passing a method to another function—such as Q.nfcall
—unbinds the method from its owner. Q has to offer its services here too so that you can avoid this, by adopting any of these two ways:
- Use
Function.prototype.bind()
- Use these methods provided by Q:
return Q.ninvoke(redisClient, "get", "user:1:id"); // node invoke return Q.npost(redisClient, "get", ["user:1:id"]); // node post
There is yet another way you can create reusable wrappers, using:
Q.denodeify
://using Q.denodeify var readFile = Q.denodeify(FS.readFile); return readFile("foo.txt", "utf-8");
Q.nbind
:// Q.nbind var redisClientGet ...
Get Mastering JavaScript Promises now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.