Асинхронизация и ожидание не работает должным образом в JavaScript - PullRequest
0 голосов
/ 24 мая 2018

Я пытаюсь выучить и использовать асинхронное - ожидание - обещание, но получаю синтаксическую ошибку "Неопределенная функция токена", ссылающуюся на строку, где я определяю свою асинхронную функцию.Я проверяю наличие открытых скобок, но все они кажутся нормальными.

Как мне заставить это работать?

------ Обновление -----

- Обработчик, который увольняется -

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

Журнал ошибок, который я получаю

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...