代码之家  ›  专栏  ›  技术社区  ›  Hackbrew

如何将承诺值返回给调用函数?

  •  0
  • Hackbrew  · 技术社区  · 8 年前

    我理解的方式是,我正在调用一个同步函数“httpget”,它应该返回一个承诺。该函数似乎正在工作,因为它通过console.log语句成功地从airtable中获取了所有数据,但在.then()方法中得到了任何响应。

    这里是我调用“httpget”函数的地方:

    async handle(handlerInput) {
        let speechText = '';
        console.log('Going to fetch Airtable data');
        await httpGet(base).then((response) => {
          console.log('have promise')    
        }).catch((err) => {
          //set an optional error message here
          console.log('do not have promise')
          //speechText = 'there is an error ' + err.message;
        }); 
        speechText = `Container ID ` + contID + ` is a ` + bincolor + `, ` + gallons + ` located in ` + binloc ;        
    
        console.log('speechText = ' + speechText);
    
        return handlerInput.responseBuilder
          .speak(speechText)
          .reprompt(speechText)
          .withSimpleCard('Warehouse Inventory', speechText)
          .getResponse();
    };
    

    下面是正在调用的httpget函数:

    async function httpGet(options) {
      // return new pending promise
      return new Promise((resolve, reject) => {    
    
            base('Bins').select({
              // Selecting a record in Grid view:
              maxRecords: 1,
              view: "Grid view"
            }).eachPage(function page(records, fetchNextPage) {
              // This function (`page`) will get called for each page of records.
    
              records.forEach(function(record) {
                  console.log('Container ID: ', record.get('Container ID'));
                  console.log('Gallons: ', record.get('Gallons'));        
                  console.log('Bin Color: ', record.get('Color'));
                  console.log('Location: ', record.get('Location'));        
                  console.log('Imperfections: ', record.get('Imperfections'));
                  var contID = record.get('Container ID');
                  var gallons = record.get('Gallons');
                  var bincolor = record.get('Color');
                  var binloc = record.get('Location');
                  var imper = record.get('Imperfections');
    
              });
    
              // To fetch the next page of records, call `fetchNextPage`.
              // If there are more records, `page` will get called again.
              // If there are no more records, `done` will get called.
              fetchNextPage();
    
          }, function done(err) {
              if (err) { 
                console.error(err); return; 
              }
          });    
      });
    } 
    

    最后,我尝试获取varaibles contid、gal、bincolor、binloc和imper中存储的值。我怎样才能做到这一点?

    2 回复  |  直到 8 年前
        1
  •  1
  •   vkarpov15    8 年前

    httpGet() resolve({ contID, gallons, /* ... */ }) then((response) => {}) response contID gallons this blog post

        2
  •  1
  •   Bash Lord    8 年前

        httpGet = (options) => {
        return new Promise((resolve, reject) => {
            resolve(/*return your result here*/);
            reject(/*return error here*/);
        });
    }
    
    
    //inside of some function
    let firstResult, secondResult;
    httpGet(argument)
        .then(result => {
            firstResult = result;
            //you can chain promise if u need
            return httpGet(secondArgument)
        })
        .then(result => {
            secondResult = result;
        })
        .then(() => {
            //here you can acces firstResult, secondResult variables, pass them to some function as arguments
        })
        .catch((error)=>{})