node.js - Set object property after asynchronous call in node -


how create nodejs module sends post request url , returns object who's data property set data returned post request?

var client = require('node-rest-client').client();  module.exports = (function(){     var instance;      function createinstance(){         client.post(url,function(data){             //set instance.data = data or             // return function lets me access private property 'data' set data returned in callback         });     }      return {         cloud : function(){             if(!instance){                 instance = createinstance();             }              return instance;         }     } }) 

edit:

basically when call above module , execute cloud function :

var cloud = require('module').cloud(); console.log(cloud.data); //should give me data received post request. //also want using module pattern, post request inside module.

you use promise e.g. :

function postreq() {     return new promise(function(resolve, reject) {        client.post(url, function(data) {            resolve(data);        })     }); }  postreq() .then(function(data) {   // whatever want data returned client.post   console.log(data);   }) 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -