Promise resolves value but log show Fulfilled as false #660

83 views
Skip to first unread message

Catia Matos

unread,
Jun 7, 2017, 12:37:28 PM6/7/17
to nodejs

I have the Ubus.ucirequest function and Im returning a promise and is resolving the value but in the end when i log the value i get promise {"isFulfilled":false,"isRejected":false}

Because of this the rest of my code is not working... In my uci function when I try to resolve another two promises and push them into an array when I iterate over the values the array is empty {} but before I do Promise.all(promises) when I log the value of promises I get

2|wscontro | [2017-06-07 15:11:06.610] - debug: /opt/wscontroller/wscontroller-api/routes/services ServicesController NA uci promises 1 [{"isFulfilled":false,"isRejected":false},{"isFulfilled":false,"isRejected":false}]
    UbusController.prototype.uciRequest = function (procedure, signature, device) {
    createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'inicio');
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'device id', device.id);
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'procedure', procedure);
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'signature', signature);

    var promise = new Promise(function (resolve, reject) {
        Controllers.Ubus.getSession(device.id).then(function (dataAuth) {
            createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'dataAuth', dataAuth);
            if (dataAuth) {
                Controllers.Ubus.execCommand(device.id, "uci", procedure, signature).then(function (data) {
                    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'data', data);
                    var res=data;
                    if (data.result) {
                        if (data.result[0] == 0) {
                            createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'promise resolved');
                            resolve(data.result[0]);
                        }
                    } else {
                        reject("no data");
                    }
                });
            } else {
                reject("no data auth");
            }
        }).catch(function (e) {
            createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', e);
            reject(e);
        });
    });
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'promise', promise);
    return promise;    
}

ServicesController.prototype.uci = function(device, config, path, section, property, value, apply, commit){
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'inicio');

    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'config', config);
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'path', path);
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'section', section);
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'option', property);
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'value', value);
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'apply', apply);
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'commit', commit);

    var values = {};
    values[property] = value;
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'values', values);

    return Controllers.Ubus.uciRequest('set', {"config": config, "section": section, values}, device)
      .then(function (uciData) {
        createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'uciData',uciData );
        var promises = [];

        if (uciData!=null) {
            createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'depois do if do uciData' );

            if (commit){
                createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'commit');
                var p1 = Controllers.Ubus.uciRequest('commit', {"config": config}, device)
                  .then(function (dataCommit) {
                    if (dataCommit && dataCommit.hasOwnProperty('result') && data.result[0] == 0) {
                      createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'commit data', dataCommit);
                    }
                  })
              promises.push(p1);
            }

            if(apply){
                createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'apply');
                var p2 = Controllers.Ubus.fileExec(device.id, "exec", path, "restart")
                  .then(function (dataApply) {
                    if (dataApply && dataApply.hasOwnProperty('result') && data.result[0] == 0) {
                      createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'apply data', dataApply);
                    }
                })
              promises.push(p2);
            }
        }

        createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'promises 1', promises);
        return Promise.all(promises).then(function(values){
            createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'promises 2', values);
        }).catch(function (err) {
            createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'error promise all', err); 
        });
        //the function is going to return an array like [dataCommit, applyCommit] or [undefined, undefined] depend of the commit / apply
    }).catch(function (e) {
        createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'error', e); 
    });
}

Stefan Klein

unread,
Jun 7, 2017, 1:14:52 PM6/7/17
to nod...@googlegroups.com
Hi,

see inline comments.

this function doesn't return a promise.
                    if (dataCommit && dataCommit.hasOwnProperty('result') && data.result[0] == 0) {
                      createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'commit data', dataCommit);
                    }
                  })
              promises.push(p1);
            }

            if(apply){
                createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'apply');
                var p2 = Controllers.Ubus.fileExec(device.id, "exec", path, "restart")
                  .then(function (dataApply) {
this neither.
                    if (dataApply && dataApply.hasOwnProperty('result') && data.result[0] == 0) {
                      createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'apply data', dataApply);
                    }
                })
              promises.push(p2);
            }
        }

        createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'promises 1', promises);
        return Promise.all(promises).then(function(values){
            createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'promises 2', values);
so your values here should be an array with 0 - 2 undefined elements.
 

tpx1

unread,
Jun 10, 2017, 12:46:53 AM6/10/17
to nod...@googlegroups.com
Hi,

you are logging promise 1 after generating these. In this state they are
not fullfilled. So this log should happen:

createLog('debug', __dirname, __filename.slice(__dirname.length + 1,
-3), null, 'uci', 'promises 2', values);

Your code is a little bit hard to read. Try to use a constant for:

__filename.slice(__dirname.length + 1, -3)

A better solution is a log function like this

const log = (dir, file) => {
const file_replaced = __filename.slice(__dirname.length + 1, -3);
return (type, ...args) => {
args.unshift(dir, file_replaced)
console.log(type, ...args)
}
}

You init this log in the first line like this:

const createLog = log(__dirname, __filename);

and then use it like:

createLog('info', null, 'uci', 'apply data', dataApply);

I will also mention that your two promises did not return any data. So
after resolving these, they will be have an undefined value. So this line:

createLog('debug', __dirname, __filename.slice(__dirname.length + 1,
-3), null, 'uci', 'promises 2', values);

should return an array with undefined as values.

Hope it will help a little bit.
> --
> Job board: http://jobs.nodejs.org/
> New group rules:
> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
> Old group rules:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> ---
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to nodejs+un...@googlegroups.com
> <mailto:nodejs+un...@googlegroups.com>.
> To post to this group, send email to nod...@googlegroups.com
> <mailto:nod...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nodejs/fc66d45d-8242-48bd-8904-9a1ad705f028%40googlegroups.com
> <https://groups.google.com/d/msgid/nodejs/fc66d45d-8242-48bd-8904-9a1ad705f028%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages