使用异步/等待仍然返回未定义

时间:2018-11-20 02:44:38

标签: javascript node.js asynchronous

我最近问了有关JavaScript request-promise module returns undefined中异步的问题。在有人提出相同的How do I return the response from an asynchronous call?这样的SO问题后,我非常理解。我学到了很多东西,可以帮助我正确地请求一个api。但是,即使我已经在使用async/await,现在我仍然遇到相同的问题。我在获得undefined的地方添加了评论。

const request = require('request');
const rp = require('request-promise');

const MongoClient = require('mongodb').MongoClient;
const ObjectId = require('mongodb').ObjectID;

const CONNECTION_STRING = process.env.DB;
const dbName = 'fcc';
const connectionOption = { useNewUrlParser: true };

function StockHandler() {

  this.latestWithoutLike = async function(ticker, multipleStock, callback) {
    if (!multipleStock) {
      console.log('Single stock');
      let lastPrice = await getLastPrice(ticker);

      if (typeof lastPrice === 'string') {
        getLikes(ticker, false, (data) => {

          let stockData = {
            stock: data.stock,
            price: lastPrice,
            likes: data.likes
          }

          return callback({ stockData: stockData });
        });
      }
    } else {
      console.log('Multiple stock');
      let firstStockLastPrice = await getLastPrice(ticker[0]);
      let secondStockLastPrice = await getLastPrice(ticker[1]);

      if (typeof firstStockLastPrice === 'string' && typeof secondStockLastPrice === 'string') {
        let firstStockLikes = await getLikes(ticker[0], false, (data) => { return data; });
        let secondStockLikes = await getLikes(ticker[1], false, (data) => { return data; });

        console.log(firstStockLikes); // <--- undefined
        console.log(secondStockLikes); // <--- undefined
      }

    } 
  };

  this.latestWithLike = async function(ticker, multipleStock, callback) {
    if (!multipleStock) {
      console.log('Single stock');
      let lastPrice = await getLastPrice(ticker);
      console.log(lastPrice);
      if (typeof lastPrice === 'string') {
        getLikes(ticker, true, (data) => {

          let stockData = {
            stock: data.stock,
            price: lastPrice,
            likes: data.likes + 1
          }

          return callback({ stockData: stockData });
        });
      }

    } else {
      console.log('Multiple stock');
      let firstStockLastPrice = await getLastPrice(ticker[0]);
      let secondStockLastPrice = await getLastPrice(ticker[1]);
      console.log(firstStockLastPrice);
      console.log(secondStockLastPrice);
    }
  };  

}

function getLastPrice(ticker) {
  let options = {
    uri:  `https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=${ticker}&interval=1min&apikey=${process.env.ALPHA_KEY}`,
    method: 'GET',
    json: true
  }

  return rp(options)
    .then(function(parsedBody){
      let latestStockTradeTime = Object.keys(parsedBody[Object.keys(parsedBody)[1]])[0];
      let closingPrice = parsedBody[Object.keys(parsedBody)[1]][latestStockTradeTime]['4. close'];
      return closingPrice;
    })
    .catch(function(error){
      return error;
    })
}

function getLikes(ticker, likeStatus, callback) {
  MongoClient.connect(CONNECTION_STRING, connectionOption, (err, client) => {

    if (err) callback(err);

    const db = client.db(dbName);
    const collection = db.collection('stock');

    if (!likeStatus) {
      try {
        collection.findOne(
          { stock: ticker.toUpperCase() },
          { upsert: true },
          (err, result) => {
            if (result === null) {
              collection.insertOne(
                { stock: ticker.toUpperCase(), likes: 0 },
                { upsert: true },
                (err, result) => {
                  return callback(result.ops[0]);
                }
              ); 
            } else {
                return callback(result);
            }
          }
        );
      } catch (e) {
          return callback({ error: e });
      }  
    } 
    else {
      try {
        collection.findOneAndUpdate(
          { stock: ticker.toUpperCase() },
          { $inc : { likes: 1 } },
          { upsert: true, new: true },
          (err, data) => {
            return callback(data.value);
          }
        );
      } catch (e) {
          return callback({ error: e });
      }
    }

  });
};

module.exports = StockHandler;

1 个答案:

答案 0 :(得分:2)

如果您要定义a function with asynchronous behavior,则可以使用async/await或Promise链接。在异步函数内部,您可以使用await或链.then()来等待异步响应。您的函数将带有已解决的值或错误的Promise返回给其调用方。

async function getLikes() {
  const likes = await db.get('myLikes'); // this db method returns a Promise
  // ...
  return likes; // will return a Promise resolving likes from db or an error if there is one
}

async function logLikes() {
  const result = await getLikes();
  console.log(result);
}

如果您使用的是异步函数,并且不像本例中那样等待或链接响应...

async function getLikes() {
  const likes = db.get('myLikes'); // uh oh, this is asynchronous
  // ...
  return likes;
}

...在将返回值分配给like之前,线程可以继续前进。那可能是您遇到undefined问题的地方。