-4

New to Promises in serverside (Node js). Here is my question

I have code in FileA.js

    incoming_form = client_services.pullForm('vsl_video1.mp4');
     incoming_form.resolve().then(function(results) {
      return console.log(results);
    });

The incoming_form returns a Promise something like this in FileB.js

    incoming_form () {
     return Promise (function (resp, rej) {
        var myArray = ['fudge', 'fgfdgdf'];
          return resp(myArray);
      });
    }

How can I read my 'myArray' value in FileA.js? when I ran my FileA.js, I am getting this error TypeError: incoming_form.resolve is not a function

JBone
  • 1,724
  • 3
  • 20
  • 32
  • if you have a promise object, you do not need to call .resolve() just do promiseObj,then( function.. ) – Nick D Apr 05 '16 at 13:26
  • try `return resp(myArray);` in incomming_form – cl3m Apr 05 '16 at 13:26
  • 2
    There's so many examples of working with promises out there on the web. – Evan Trimboli Apr 05 '16 at 13:28
  • 1
    Why do you use `Promise` in this case? – Lewis Apr 05 '16 at 13:29
  • Possible duplicate of [How to access the value of a promise?](http://stackoverflow.com/questions/29516390/how-to-access-the-value-of-a-promise) – Jared Smith Apr 05 '16 at 13:29
  • Cl3m, sorry it was returning rest(myArray) as you stated, but I am getting this error "TypeError: incoming_form.resolve is not a function
    " when I do incoming_form.resolve().then(function(results) { return console.log(results); });
    – JBone Apr 05 '16 at 13:36
  • Tresdin, The incoming_form() is returned by some one else already. I just have to use it and see how I can print the array from the returned Promise. – JBone Apr 05 '16 at 13:45

2 Answers2

1

I think you may have misunderstood the internal mechanisms by which Promise objects operate. This is how your code should look:

function incoming_form () {
 return new Promise (function (resolve, reject) {
    resolve(['fudge', 'fgfdgdf']);      
  });
}

incoming_form().then(function(results){
    console.log ("Results" + results");
})

Note how the promise resolves internally, as opposed in a function call. Whenever a promise resolves, operation (and the arguments that the promise resolves with, in this case an array) is deferred to the closest subsequent then() clause.

Hope this clears some things up!

jonny
  • 3,022
  • 1
  • 17
  • 30
1

Use response.json() and then to return the value from promise object.

    fetch('http://localhost:3100/comments')
    .then(response => response.json())
    .then(comments => console.log(comments));
mahesh5b5
  • 32
  • 5