函数外的返回值

Returning value outside of function

我正在寻找使用 graphQL 来查询 docker 机器 api 并获取 React docker 管理风格项目的容器列表。我正在使用 dockerode NPM 模块来发出请求。第一个函数 getCOntainerById 是我通常 return 来自 rethinkdb 的一些项目的方式。

我似乎无法弄清楚如何 return docker.listContainers 函数中的容器数组,因为它仅在范围内定义,而在 fetchContainers 函数的末尾未定义 return.

import Docker from 'dockerode'
var docker = new Docker({host: 'http://127.0.0.1', port: 52376});

export default {
  getContainerById: {
    type: Container,
    args: {
      id: {type: new GraphQLNonNull(GraphQLID)}
    },
    async resolve(source, {id}, {rootValue}) {
      isLoggedIn(rootValue);
      const container = await r.table('containers').get(id);
      if (!container) {
        throw errorObj({_error: 'Container not found'});
      }
      return container;
    }
  },
  fetchContainers: {
    type: new GraphQLList(Container),
    async resolve(source, {id}, {rootValue}) {
      isLoggedIn(rootValue);
      docker.listContainers(function(err, containers) {

      });

      if (!containers) {
        throw errorObj({_error: 'Container not found'});
      }

      return containers
    }
  }
};

将不胜感激。谢谢。

async resolve(source, {id}, {rootValue}) {
  isLoggedIn(rootValue);
  const containers = await new Promise((resolve, reject) => {
    docker.listContainers((err, containers) => {
      if (err) reject(err);
      resolve(containers);
    });
  })

  if (!containers) {
    throw errorObj({_error: 'Container not found'});
  }

  return containers
}