Функция возврата обещания Node.js Wit.ai

Мое обещание возвращает неопределенные значения. Я не уверен, как правильно выполнить эту функцию на моей странице node.js. Не могли бы вы помочь мне с этим, чтобы вернуть все значения ПОСЛЕ геокодирования в моем контексте в хорошем смысле! Спасибо

merge_location({
    entities,
    context,
    message,
    sessionId
}) {
    return new Promise(function(resolve, reject) {
        var location = firstEntityValue(entities, 'location');
        if (location) {
            geocoder.geocode(location).then(function(res) {
                console.log(res);
                context.location = location;
                context.lat = res[0].latitude;
                context.lng = res[0].longitude;
                delete context.MissingLocation;
            }).catch(function(err) {
                context.MissingLocation = true;
                delete context.location;
                delete context.lat;
                delete context.lng;
                console.log("Il n'y a pas de ville ");
            });
        } else {
            context.MissingLocation = true;
            delete context.location;
            delete context.lat;
            delete context.lng;
            console.log("Il n'y a pas de ville ");
        }
        console.log("I want to return this" + context.location + ' ' + context.lat + ' ' + context.lng);
        return resolve(context);
    });
}

person Pablo DelaNoche    schedule 17.01.2017    source источник


Ответы (1)


Вам не нужно говорить return resolve(.... Вместо этого вы просто звоните resolve(...

Например:

function myPromiseFunction(x) {
    return new Promise(function (resolve, reject) {
        // Do your lengthy processing with x here

        if (everyThingLooksGood) {
            resolve(thingYouWantToReturn);
        } else {
            reject("ERROR: Things didn't go according to plan!");
        }
    });
}
person Mitchell Griest    schedule 22.04.2017