Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
异步和等待在javascript中无法正常工作_Javascript_Alexa Skills Kit - Fatal编程技术网

异步和等待在javascript中无法正常工作

异步和等待在javascript中无法正常工作,javascript,alexa-skills-kit,Javascript,Alexa Skills Kit,我试图学习和使用async-await-promise,但是我遇到了一个语法错误“Undefined token function”,它指的是我定义异步函数的那一行。我检查了一个开放的括号,但他们似乎都很好 我怎样才能让它工作 ----更新--- const NumberIntentHandler = { canHandle(handlerInput) { return handlerInput.requestEnvelope.request.type === 'IntentRequ

我试图学习和使用async-await-promise,但是我遇到了一个语法错误“Undefined token function”,它指的是我定义异步函数的那一行。我检查了一个开放的括号,但他们似乎都很好

我怎样才能让它工作

----更新---

const NumberIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name === 'NumberIntent';
  },
  async handle(handlerInput) {
      let slotNum = handlerInput.requestEnvelope.request.intent.slots.number.value;
      //var myRequest = parseInt(slotNum);   
      const myRequest = parseInt(slotNum);
      console.log('NumberIntentHandler myRequest: ', myRequest);
      const options = `http://numbersapi.com/${myRequest}`;
      console.log('NumberIntentHandler options: ', options);

      // Use the async function
  const myResult = await httpGet(options)
         console.log("sent     : " + options);
         console.log("received : " + myResult);
         const speechText = myResult;
         console.log('speechText: ', speechText); // Print the speechText   */ 

      return handlerInput.responseBuilder
           .speak(speechText)
           .withSimpleCard('Here is your fact: ', speechText)
           .getResponse(); 
  },
};
async function httpGet(options) {
  // return new pending promise
  console.log(`~~~~~~~~ httpGetPromise ~~~~~~~~~`);
  console.log(`~~~~~~~~~~~~~~~~${JSON.stringify(options)}~~~~~~~~~~~~~~`);


  return new Promise((resolve, reject) => {
    const request = http.get(options, (error, response, body) => {
      // handle http errors
      if (response < 200 || response > 299) {
        reject(new Error('Failed to load page, status code: ' + response));
      }

    });
    // handle connection errors of the request
    request.on('error', (err) => reject(err));
    request.end();
  });
} 
--被解雇的处理程序--

const NumberIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name === 'NumberIntent';
  },
  async handle(handlerInput) {
      let slotNum = handlerInput.requestEnvelope.request.intent.slots.number.value;
      //var myRequest = parseInt(slotNum);   
      const myRequest = parseInt(slotNum);
      console.log('NumberIntentHandler myRequest: ', myRequest);
      const options = `http://numbersapi.com/${myRequest}`;
      console.log('NumberIntentHandler options: ', options);

      // Use the async function
  const myResult = await httpGet(options)
         console.log("sent     : " + options);
         console.log("received : " + myResult);
         const speechText = myResult;
         console.log('speechText: ', speechText); // Print the speechText   */ 

      return handlerInput.responseBuilder
           .speak(speechText)
           .withSimpleCard('Here is your fact: ', speechText)
           .getResponse(); 
  },
};
async function httpGet(options) {
  // return new pending promise
  console.log(`~~~~~~~~ httpGetPromise ~~~~~~~~~`);
  console.log(`~~~~~~~~~~~~~~~~${JSON.stringify(options)}~~~~~~~~~~~~~~`);


  return new Promise((resolve, reject) => {
    const request = http.get(options, (error, response, body) => {
      // handle http errors
      if (response < 200 || response > 299) {
        reject(new Error('Failed to load page, status code: ' + response));
      }

    });
    // handle connection errors of the request
    request.on('error', (err) => reject(err));
    request.end();
  });
} 
--从处理程序调用的函数--

const NumberIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name === 'NumberIntent';
  },
  async handle(handlerInput) {
      let slotNum = handlerInput.requestEnvelope.request.intent.slots.number.value;
      //var myRequest = parseInt(slotNum);   
      const myRequest = parseInt(slotNum);
      console.log('NumberIntentHandler myRequest: ', myRequest);
      const options = `http://numbersapi.com/${myRequest}`;
      console.log('NumberIntentHandler options: ', options);

      // Use the async function
  const myResult = await httpGet(options)
         console.log("sent     : " + options);
         console.log("received : " + myResult);
         const speechText = myResult;
         console.log('speechText: ', speechText); // Print the speechText   */ 

      return handlerInput.responseBuilder
           .speak(speechText)
           .withSimpleCard('Here is your fact: ', speechText)
           .getResponse(); 
  },
};
async function httpGet(options) {
  // return new pending promise
  console.log(`~~~~~~~~ httpGetPromise ~~~~~~~~~`);
  console.log(`~~~~~~~~~~~~~~~~${JSON.stringify(options)}~~~~~~~~~~~~~~`);


  return new Promise((resolve, reject) => {
    const request = http.get(options, (error, response, body) => {
      // handle http errors
      if (response < 200 || response > 299) {
        reject(new Error('Failed to load page, status code: ' + response));
      }

    });
    // handle connection errors of the request
    request.on('error', (err) => reject(err));
    request.end();
  });
} 
异步函数httpGet(选项){ //返回新的待定承诺 console.log(`httpGetPromise~~~~~~~~~~~~~~~~~`); console.log(`~~~~~~~~~~~~~~~~~${JSON.stringify(选项)}~~~~~~~~~~~~~`); 返回新承诺((解决、拒绝)=>{ const request=http.get(选项,(错误、响应、正文)=>{ //处理http错误 如果(响应<200 | |响应>299){ 拒绝(新错误('加载页面失败,状态代码:'+响应)); } }); //处理请求的连接错误 请求.on('error',(err)=>拒绝(err)); request.end(); }); } 我收到的错误日志


@AlexDovzhanyn正常,但仍然没有。
异步函数
声明工作正常。@Hackbrew您在什么环境下运行此代码,node.js?什么版本?它是否支持
async
/
await
语法?@Hackbrew Then。更新至7或更高版本。谢谢,在我们将您的问题确定为节点版本后,不再需要完整的文件。我认为您确实应该关注该特定问题,而不是不断编辑此问题。另外,请不要将错误消息作为截图发布。