回调太多.. nodeJS airtbale api

时间:2016-10-06 09:59:13

标签: node.js callback promise airtable

首先,我是NodeJS的新手,想要提高我的技能。 我在Airtable有一张桌子,想要从中获取所有元素。 使用nodejs的airtable api很容易。 但我想要做的是将这些元素推送并保存在未来的选项卡中(JSON,excel ......)。 为了做到这一点,我使用的是callbaks,因为调用是异步的..我听说过Promises,但这对我来说很新,而且我很难理解它。 这是我现在的代码:

var Airtable = require('airtable'); 
Airtable.configure({
    endpointUrl: 'https://api.airtable.com',
    apiKey: 'keyKWYJPOEObWhNt2'
});
var base = Airtable.base('app4qIwfmG0ZKAdBH');
var view = "Main View";


var tab = [];
base('Table 1').select({
    view : view}).eachPage(function page(records, fetchNextPage){records.forEach(function(record){
        tab.push({
            "Name": record.get('Name'), 
            "Notes": record.get('Notes')
        });
    });
    fetchNextPage();
    pushToArray(tab);
}, function done (error){
    if(error){ console.log(error);
        console.log(tab);}
});

function pushToArray(tab) {
    TabToJson(tab);
    return tab;
};

function TabToJson(tab){
    console.log(tab);
    return JSON.stringify(tab); 
};

我如何实施承诺?这有必要吗?我不想结束十几个回调函数.. 谢谢大家,祝你有个美好的一天!

2 个答案:

答案 0 :(得分:3)

Careful here! You're on the right track with realizing that this function is async and that you want to wait until every iteration of #eachpage has resolved before writing outputting your JSON, like a Promise would. But Airtable was kind enough to already provide what you're looking for: the callback

function done (error){
  if(error){ console.log(error);
    console.log(tab);
  }
}

will run immediately after the last successful call to #fetchNextPage. This is where you should have your JSON-writing logic. You would want something like

function done (error){
  TabToJson(tab);

  if(error){ console.log(error);
    console.log(tab);
  }
}

You don't need your function pushToArray, as you've already pushed the individual records from Airtable into your array 'tab' in each call to #page. Furthermore, if you want to do more than log your JSON output, which your question makes it seem, you should look into Node's File System Library. checkout the fs#writeFile method.

答案 1 :(得分:0)

您可以使用异步/等待。请确保您可以在异步函数中使用await。

version: "2"
services:
  rasa:
    image: registry.gitlab.com/xhub-org/p/xpeers/slackbot:{{img_version}}
    container_name: rasa
    labels:
      - traefik.frontend.rule=Host:rasa.dev.x-hub.io
      - traefik.port={{app_port}}
      - traefik.docker.network=web
      - traefik.enable=true
    restart: always
    networks: ['rasa-network']
    command:
      - run
      - --cors
      - "*"
      - --enable-api
      - --log-file
      - out.log
      - --connector
      - slack
      - --credentials
      - slack_credentials.yml
      - --endpoints
      - endpoints.yml
      - -m
      - /models
    depends_on:
      - action_server

  action_server:  
    image: registry.gitlab.com/xhub-org/p/xpeers/slackbot:{{img_version}}
    container_name: rasa_action_1
    networks: ['rasa-network']
    command :
      - run
      - actions

  duckling:
    image: rasa/duckling:latest
    networks: ['rasa-network']
    ports:
      - "8005:8005"

networks: {rasa-network: {}}