承诺未决;承诺函数不会做任何回调

时间:2017-11-13 21:50:33

标签: javascript node.js promise

我正在调用post('/audit'),然后调用getSignature (Promise) function,它会返回signature的正文以打印出来(使用console.log(audit_signature)。但是,我的Promise仍为pending,我的audit_signature仍然未定义。是什么导致了promise和audit_signature的问题?如何解决?

(错误):

start /audit
[stdout] jwtPayload: { description: 'admin',
[stdout]   id: '1508959916',
[stdout]   signature: '4d4a542f-d738-4736-8df5-84575b4d90fd' }
[stdout] ids:  4d4a542f-d738-4736-8df5-84575b4d90fd
[stdout] start: getSignature
[stdout] getAudit: Promise { <pending> }
[stdout] audit_signature: undefined
[stdout] start: responseWithJSON

(server.js)

 function getSignature(uuid){
    console.log('start getSignature');
     return new Promise(function (resolve, reject){
        request.post({
            url: `http://${ksiIP}:${ksiPort}/KsiProxy/rest/util/getSignature`, 
            headers: {
                'Content-Type': 'application/json',
                'Authorization' : 'Basic Og=='
            }, json: {'uuid': uuid}
        }), function(error, response, body){
            console.log(error);
            console.log(response);
            console.log(body);

            if(error || (response.statusCode != 200)){
                return reject((error ? error : body));
            }
            if(body.resultcode != 0){
                console.log("Error: " + requestResult.info);
                console.log("Asset could not be verified.");
                return reject(new Error(body.info));
            }
            if(response == 'undefined') {
                console.log('ksi unavailable');
                return reject(error);
            }
            //add identification as well
            resolve(body.signature);
            //resolve('resolve is here');
        }
    })      
}

dcServer.post('/audit', function(req,res){ 
    console.log('start /audit'); 
    if(req.jwtPayload.description != "admin"){
        respondWithJSON(401, 401, "Not authorised for this functionality.", res);
        return;
    }
    let toAudit = req.jwtPayload.signature; 
    var audit_signature;
    console.log('jwtPayload:', req.jwtPayload);
    console.log("ids: ", toAudit);

    let failed = 0;//counter to increase if signatures couldnt be retreived. 

    var getAudit = getSignature(toAudit).then( 
                (result) => {return audit_signature = result;} ,
                (error) => {
                        failed++;
                        audit_signature = 'unavailable';
                        console.log(error);
        });
    console.log('getAudit:', getAudit);
    console.log('audit_signature:', audit_signature);

   });

1 个答案:

答案 0 :(得分:0)

getAudit是承诺,当您打印它时,您会看到getAudit: Promise { <pending> } 并且getSignature是异步的,因此您必须等到承诺解析并打印audit_signature,如下所示:

 var getAudit = getSignature(toAudit).then( 
                (result) => {return audit_signature = result;} ,
                (error) => {
                        failed++;
                        audit_signature = 'unavailable';
                        console.log(error);
        });

    getAudit.then(audit_signature =>{
        console.log('audit_signature:', audit_signature);
    })