首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Node.js RESTAPI REsponse在执行下一步之前等待

Node.js RESTAPI REsponse在执行下一步之前等待
EN

Stack Overflow用户
提问于 2017-09-01 09:53:59
回答 1查看 386关注 0票数 0

我在做亚马逊莱克斯的工作。在Amazon中,我编写了一个Node.js函数,它调用一个Openweather调用。这是函数。

代码语言:javascript
复制
function todaysweather(intentRequest, callback, data) {
    const location = intentRequest.currentIntent.slots.location;
    const source = intentRequest.invocationSource;
  const outputSessionAttributes = intentRequest.sessionAttributes || {};
  const temp = JSON.parse(outputSessionAttributes.temp || '{}');
  ***console.log("Inside perform request");***
  var endpoint = '/data/2.5/weather?q=${location}&appid=234426bef0d81ef4474073344f';
  var method = 'POST';
  var dataString = JSON.stringify(data);
  var headers = {};
  var responseObject;

  if (method == 'GET') {
    endpoint += '?' + querystring.stringify(data);
  }
  else {
    headers = {
      'Content-Type': 'application/json',
      'Content-Length': dataString.length
    };
  }
  var options = {
    host: host,
    path: endpoint,
    method: method,
    headers: headers
  };
  ***console.log("Before http perform request");***
  var req = https.request(options, function(res) {
    res.setEncoding('utf-8');
    var responseString = '';
    res.on('data', function(data) {
      responseString += data;
    });
  ***console.log("before perform response");***
    res.on('end', function() {
      console.log(responseString);
      responseObject = JSON.parse(responseString);
      var tempVAR = responseObject.main.temp;
      console.log("*************" + tempVAR);
      //success(responseObject);
    });
  });

  req.write(dataString);
  req.end();

  ***console.log("before callback request");***
    callback(close(outputSessionAttributes, 'Fulfilled', { contentType: 'PlainText',
       content: `Okay, I have booked your appointment.  We will see you` }));
}

API调用需要几毫秒的时间才能响应.在那之前,我的下一个代码是执行..。如何阻止它被处决。如果您查看下面的日志“回调请求前”是在“响应请求前”之前执行的,请帮助我了解如何解决这个问题?

启动RequestId: 6 8a17 856-8 ef8-11e7-8a17-afa62db3dcddc版本:$LATEST09:32:13 2017-09-01T09:32:13.734Z 6fafb856-8 ef8-11e7-8a17-afa62db3dcdc event.bot.name=TodaysWeather09:32:13 2017-09-01T09:32:13.735Z 6fafb856-8 ef8-11e7-8a17-afdb623 01T09调度userId=yczl74p0he593v0kduouy5c5nhci50e5,intentName=Todaysweather09:32:13 2017-09-01T09:32:13.735Z 6fafb856-8 ef8-11e7-8a17-afa62db3dcdc09:32:13 2017-09-01T09:32:13.735Z 6fafb856-8 fafb856-8-8a17-afadb3 201709:32:13 2017-09-01T09:32:13.975Z 6fafb856-8ef8-11e7-8a1709-01T09:32:14.190Z 6fafb856-8 ef8-11e7-8a17-afa62db3dcdc09:32:14 2017-09-01T09:32:14.193Z 6fafb856-8 ef8-11e7-8a17-afa62db3dcdc {"coord":{"lon":-71.32,"lat":44.63},“天气”:{“id”:804,“主”:“云”,“描述”:“阴云”,“图标”:“04n”},“基础”:“台站”,“主”:{“临时”:280.34,“气压”:1015,“湿度”:70,"temp_min":279.15,"temp_max":281.15},“能见度”:16093,“风”:{“速度”:2.6,"deg":250},“云”:{“所有”:90},"dt":1504257840,"sys":{"type":1,"id":195609:32:14 2017-09-01T09:32:14.233Z 6fafbb856-8 faf8-11e7-8a17-afa62db3dcdc*09:32:14结束:6fb856-8e8-11e7-8a17-afa62db3dcdc

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-01 10:36:24

是的,这是因为Node.js的异步特性。您将发送请求,并在此之后调用回调,而无需等待响应。

要等待响应,需要在res.on('end',..__的回调中调用lambda回调。类似于:

代码语言:javascript
复制
res.on('end', function() {
  responseObject = JSON.parse(responseString);
  callback(close(outputSessionAttributes, 'Fulfilled', { contentType: 'PlainText', content: `Okay, I have booked your appointment.  We will see you` }));
});

在这种情况下,只有在从API接收到响应后才会调用lambda回调。

希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45997604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档