-1

Hi there im new to writing promises in javascript. I want to return a value from func1 which are composed of then (using q) calling other functions resolving the value then passing to through the next function. The problem is i want to return the last value in func 1. So I can use it in caller function. But init value just returns undefined. Here are the codes:

function func1(string1, string2) {
  othermodule
    .otherfunc1(string1)
    .then(function(outputstring1) {
      var params = othermodule.otherfunc2(outputstring1,string2);
      return params;
    })
    .then(anotherfunc)
    .then(anotherfunc2)
    .then(function (data) {
       console.log(data);
       // outputs data
       return data;
    });
}

function caller() {
  var initValue = 0;
  initValue = func1(string1,string2);
  console.log('init value = '+initValue);
  //init value = undefined
}
PSWai
  • 1,198
  • 13
  • 32
Kenichi Shibata
  • 148
  • 2
  • 11

2 Answers2

3

Writing asynchronous code in javascript is poisonous, meaning all code calling async code must be async itself.

Your code can be rewritten to:

function func1(string1, string2) {
    return Q.fcall(othermodule.otherfunc1, string1)
        .then(function(outputstring1) {
            var params = othermodule.otherfunc2(outputstring1, string2);
            return params;
        })
        .then(anotherfunc)
        .then(anotherfunc2)
        .then(function(data) {
            console.log(data);
            return data;
        });
}

function caller() {
    return func1(string1, string2).then(function(initValue) {
        console.log('init value = ' + initValue);
    });
}
Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97
1

Return the promise in func1

and use .then in caller to get the "returned" value

function func1(string1, string2) {
    return othermodule.otherfunc1(string1)
        .then(function(outputstring1) {
            var params = othermodule.otherfunc2(outputstring1, string2);
            return params;
        })
        .then(anotherfunc)
        .then(anotherfunc2)
        .then(function(data) {
            console.log(data);
            return data;
        });
}

function caller() {
    func1(string1, string2).then(function(initValue) {
        console.log('init value = ' + initValue);
    });
}
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
  • sorry got it now I forgot to put return on the othermodule.otherfun1(string1) thanks! :) – Kenichi Shibata Dec 09 '15 at 02:39
  • the problem is I want to get the data outside the promise. Is there no method to return this? If I do the way you did then I will still be inside the promise. – Kenichi Shibata Dec 09 '15 at 03:08
  • You have to deal with the asynchronous nature of the code - you can't turn it synchronous. Embrace the asynchronous nature of javascript, it's not that hard to change how you code – Jaromanda X Dec 09 '15 at 04:47
  • @user1727843 - is there a problem with the answer or how you are using it? – Jaromanda X Dec 09 '15 at 09:46