JS未定义 - 即使在等待一段时间后也是如此

时间:2018-02-27 16:00:10

标签: javascript node.js

我有以下函数,当我调用此函数并将其值存储在变量中时,它始终打印undefined ...但函数内的console.log()语句显示正确的数据。

我无法理解这里的原因,我正在等待执行的功能仍然输出undefined。

function getInfo(messageContent) {
    let getHttpRegex = /(?:https?):\/\/[\n\S]+/ig;
    let httpMsg = messageContent.match(getHttpRegex)
    let urlExpander = require('url-unshort')()
    let location = "", lat = 0, lng = 0, encounterId = 0;

    if (httpMsg[0].toString().includes('tinyurl')) {
        urlExpander.expand(httpMsg[0])
            .then(expandedUrl => {
                if (expandedUrl) {
                    console.log(`Original url is: ${expandedUrl}`);
                    location = /lat=([-]?\d+\.\d+)&lon=([-]?\d+\.\d+)/.exec(expandedUrl)
                    encounterId = /&encId=(\d+)/.exec(expandedUrl)
                    lng = Number(location.pop()).toFixed(6);
                    lat = Number(location.pop()).toFixed(6);
                    encounterId = encounterId[1]
                    console.log([lat, lng, encounterId]) //Prints perfect
                    return [lat, lng, encounterId]
                }
                // no shortening service or an unknown one is used
                else console.log("This url can't be expanded");
            })
            .catch(err => console.log("err " + err));
    } else {
        location = /lat=([-]?\d+\.\d+)&lon=([-]?\d+\.\d+)/.exec(httpMsg[0])
        encounterId = /&encId=(\d+)/.exec(httpMsg[0])
        lng = Number(location.pop()).toFixed(6);
        lat = Number(location.pop()).toFixed(6);
        encounterId = encounterId[1]
        console.log([lat, lng, encounterId]) //Prints perfect
        return [lat, lng, encounterId]
    }
}

功能调用:

const Bluebird = require("bluebird")
let cordsAndEncId = getInfo(messageContent) // messagecontent is the string on which regex is performed

    Bluebird.delay(5000).then(delayValue => {
        console.log(cordsAndEncId) // Prints udnefined
    })

我不明白这里的错误....请指出

0 个答案:

没有答案