2017-01-17 120 views
0

我的承諾返回未定義的值。我不知道如何在node.js頁面中正確執行此功能。您能否幫我解決這個問題,以便在我的上下文中以良好的方式返回所有值!謝謝
Node.js Wit.ai承諾返回函數

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); 
    }); 
} 

回答

0

您不需要說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!"); 
     } 
    }); 
}