javascript - hoisting anonymous function expression which is an array item -


this code needs declare function (preferably anonymous) inside array
({argobj}) => {console.log(start);} , define later outside request = (function() {...}()); iif.

request = (function () {   const pathafter = {     start: ['homepage', 'get', ({argobj}) => {console.log(`start`);}]   };   return {      go: (argobj) => {       if(!pathafter[argobj.pathafter]) return;        const path = pathafter[argobj.pathafter][0];       const method = pathafter[argobj.pathafter][1];       const url = data.domain + path + data.ext;        http.call(method, url, (error, response) => {         if (error) {           console.log('error '+error);         } else {           pathafter[path][2]({response: response}); // <---- calls relevant method            request.go({pathafter: path});         }       });       return true; // if rms seccessful     }   };  }());  // function definition goes here... 

i not sure how this. thanks

i not entirely clear on trying acheive , how strict requirements are, 1 option might give request object ability add/extend handlers in pathafter:

request = (function () {     const pathafter = {         start: ['homepage', 'get', ({argobj}) => {console.log('start');}]     };     return {          go: (argobj) => {             if(!pathafter[argobj.pathafter]) return;              const path = pathafter[argobj.pathafter][0];             const method = pathafter[argobj.pathafter][1];             const url = data.domain + path + data.ext;              http.call(method, url, (error, response) => {                 if (error) {                     console.log('error '+error);                 } else {                     pathafter[path][2]({response: response}); // <---- calls relevant method                      request.go({pathafter: path});                 }             });             return true; // if rms seccessful         },          registerpathhandler: (handlername,handler)=> {             pathafter[handlername] = handler;         }      };  }());  request.registerpathhandler('test', ['testpage', 'get', ({argobj}) => {console.log('test');}]); 

this add named handler, , used add start handler well. if start handler needs hard-coded inside, code above modified replace array element instead.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -