node.js - Creating a yieldable Node Module/Object -


i trying create node module (using harmony) upon loading module/application, has yielded things in it's construct can executed , loaded before of it's exposed functions can called.

the issue having cannot seem yield internal function being executed, using module.exports. example help.

module.exports = function*(s_id){     console.log('loading module lets execute till here');     if (!(this instanceof tester)) return yield new tester();     }   function* tester(){     console.log('but never execute generator function');     }  tester.prototype = {     model : function*(){         // other functions         }     } 

it's been stumping me hours now! feel solution super simple cannot seem wrap head around it. have tried make tester() function export, still having same issue. why can't seem yield tester() function?

also, may alternative approach? want maintain object nature of module module can loaded different inputs, such s_id variable/object in example above.

a node module (using harmony) upon loading module/application, has yielded things in it's construct can executed , loaded before of it's exposed functions can called

don't that. generators not made asynchrony. yield doesn't want here. module not "yielded" await in load. yield magic, not async magic.

if must use asynchronous module loading process, export promise actual module. standard interface await, , can consumed using standardized approach not rely on internals of module.

you still can use yield syntax constructing promise, use favorite coroutine library.

return yield new tester(); … function* tester(){…} 

uh oh. yes, apparently possible call generator functions constructors. believe me, not want. constructor arbitrary object should return object, instead of iterator (just shouldn't return promise). if of object methods generator methods, that's fine, constructor should not.

if want use generator function in code (and it's not meant constructor), you

  • will need yield* iterator you've created (tester()) instead of yielding it
  • must not overwrite .prototype property did, causes iterator malfunction. (of course should never @ all, though of time works)

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -