0

I want a value from child function to the parent function in a Node.js and use that value in another file. Being asynchronous flow the output of d.promise is pending.
How can I get the value of d.promise as fulfilled with the data passed in the arangoFunction.js file in the queryCheck variable in the site.js file?

Database used : ArangoDB

site.js snippet

var arangoFunc = require('./arangoFunction'), queryCheck;
queryCheck = arangoFunc.saveData(data, roleCollection, res);
console.log(queryCheck);

arangoFunction.js snippet

var q = require('q');
var d = q.defer();
    exports.saveData = function(data, collectionName, res){
          var collectionData = db.collection(collectionName);
          collectionData.save(data, function(e, o){
              if(e) {
                console.error(e);
                res.status(e.response.statusCode).send(e.response.statusMessage);
                d.reject(false);
              }
              else {
                res.send(data);
                d.resolve(data);
              }
            });
            return d.promise;
        }

Output of site.js snippet

{ state: 'pending' }

1 Answer 1

1

arangoFunction.js

exports.saveData = function(data, collectionName, res){
  var collectionData = db.collection(collectionName);
  return q.Promise(function(resolve, reject, notify){
    collectionData.save(data, function(e, o){
      if(e) {
        console.error(e.response.statusMessage);
        res.status(e.response.statusCode).send(e.response.statusMessage);
        reject(e);
      }
      else {
        res.send(data);
        resolve(data);
      }
    });
  });
}

site.js

arangoFunc.saveData(data, roleCollection, res).then(function(value){
 // do whatever with the returned data
}, function(err){
 // error handling
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.