I am not fully sure this is the right place to discuss CommonJS patterns but I'd like to understand if there's the possibility to discuss a possible new Module.prototype method like the following one:
// Module as module.constructorModule.prototype.import=function(path){returnnewPromise(res=>res(this.require(path)));};This would enable asynchronous import and asynchronous exports out of the box.
// modules as we know: converter.jsconstcrypto=require('crypto');module.exports={sha256: (str,secret='')=>crypto.createHmac('sha256',secret).update(str).digest('hex')};// same good old synchronous flavorconstconverter=require('./converter');console.log(converter.sha256('yolo'));// module.import asynchronous flavormodule.import('./converter').then((converter)=>{console.log(converter.sha256('yolo'));});To export asynchronously the module has to simply export a Promise
// generic async exportmodule.exports=newPromise(define=>{// some asynchronous operation ...define({my:'module'});});// asynchronous converter.jsmodule.exports=module.import('./crypto').then((crypto)=>{// return the module to exportreturn{sha256:(str,secret='')=>crypto.createHmac('sha256',secret).update(str).digest('hex')};});Multiple dependencies can be easily handled via Promise.all.
module.exports=Promise.all(['./awe','./some'].map(m=>module.import(m))).then(modules=>{const[awe,some]=modules;return{awe, some};});I'm testing already these patterns on both server and client [1], and there's nothing I can spot that could cause troubles, in terms of backward compatibility, but it could enable new kind of possibilities for both client and server.
Thank you for your consideration.
[1] https://medium.com/@WebReflection/asynchronous-module-import-path-b9f56675e109#.ehnofaj2q
I am not fully sure this is the right place to discuss CommonJS patterns but I'd like to understand if there's the possibility to discuss a possible new
Module.prototypemethod like the following one:This would enable asynchronous import and asynchronous exports out of the box.
To export asynchronously the module has to simply export a
PromiseMultiple dependencies can be easily handled via
Promise.all.I'm testing already these patterns on both server and client [1], and there's nothing I can spot that could cause troubles, in terms of backward compatibility, but it could enable new kind of possibilities for both client and server.
Thank you for your consideration.
[1] https://medium.com/@WebReflection/asynchronous-module-import-path-b9f56675e109#.ehnofaj2q