1

I'm completely rewriting my question do boil it down even further.

Why, in the following code, does x() equate to undefined instead of success which is logged to the console by console.log('success');

The last line finishes executing in the console; then the .then() call back is triggered.

How can I make it so x() returns the 'success' value before the last line begins to execute.

Even yF() evaluates to undefined. However, .then() is echoing th y: success.

const promise = require('promise');
const requestify = require('requestify');


function x() {
    requestify.get('https://<redacted>/')
        .then(function (d) {
            console.log('success', d.code);
            return 'success';
        })
        .fail(function (f) {
            console.log('fail', f.code);
            return 'fail';
        });
    ;
}


var yF = function () {
    yP   .then(function (th) { console.log("th", th); return th; })
        .catch(function (fl) { console.log("fl", fl); return fl; });
}


var yP = new Promise(
    function (resolve, reject) {
        if (1 == 1) {
            resolve("y: success");
        } else {
            reject(new Error("y: fail"));
        }
    }
);




console.log("hello", x());
console.log("world", yF());
user5903880
  • 102
  • 8
  • please explain further. Are you saying that the code in the then clause of requestify.get gets fired before the request is completed? – user93 Jul 01 '17 at 15:31
  • Are you completing the request in the backed inside then after doing all operation – user93 Jul 01 '17 at 15:42

2 Answers2

0

The function x does not return a value. This example may help:

> function foo() { console.log('hi from foo'); }
undefined
> console.log('calling foo', foo());
hi from foo
calling foo undefined

You need to return the promise in your function. Function x could change like this:

function x() {
    return requestify.get('https://<redacted>/')
        .then(function (d) {
            console.log('success', d.code);
            return 'success';
        })
        .fail(function (f) {
            console.log('fail', f.code);
            return 'fail';
        });
}

Now you can call x with then:

x().then(result => assert(result === 'success'));
user5903880
  • 102
  • 8
Andy Gaskell
  • 31,495
  • 6
  • 74
  • 83
  • Maybe not for you, but for me the console.log("hello,,, x().then() resolves to `hello { state: 'pending' }`. thanks for the reminder that I can return the `requestify()` that alone turned on a lightbuld – user5903880 Jul 01 '17 at 22:59
0

Two approaches:

1) x() ~ I'll call, pass a variable forward

2) yP()~ consuming a promise

const promise = require('promise');
const requestify = require('requestify');

var f = "";


function yP() {
    return new Promise(function (resolve, reject) {
        requestify.get('https://<redacted>')
            .then(function (da) {
                var fpt = "success(yP):" + da.code.toString();
                console.log('success-yP', fpt);
                resolve(fpt);
            })
            .catch(function (ca) {
                var fpc = "fail(yP):" + ca.code.toString();
                console.log('fail-yP', fpc);
                reject(fpc);
            });
    });
}


function x() {
    requestify.get('https://<redacted>/')
        .then(function (da) {
            f = "success(x):" + da.code.toString();
            console.log('success-x', f);
            consumef();
        })
        .catch(function (ca) {
            f = "fail(x):" + ca.code.toString();
            console.log('fail-x', ca);
            consumef();
        });
    ;
}


function consumef() {
    console.log("hello", f);

}



x();
yP()
    .then(function (fyPt) { console.log('yP().then', fyPt); })
    .catch(function (fyPc) { console.log('yP().catch', fyPc); });

Debugger listening on [::]:5858

success-yP success(yP):200

yP().then success(yP):200

success-x success(x):200

hello success(x):200

user5903880
  • 102
  • 8