2

I'm trying to pull users data and than use returned result in another function. I'm trying to implement it using async/await but with no success.

  const userAllowedToDeploy = (user_id, project) => {
    controller.storage.users.get(user_id, async function(err, user) {
      result = await (some calculations related to returned user here);
      return result;
    });
   });

  controller.hears(['^deploy (\\w+)'], 'direct_mention, mention', function(bot, message) {
    let channel = message.channel;
    let project = message.match[1];

    result = userAllowedToDeploy(message.user, project)
    console.log('final result: ' + result);

But for some reason final result is undefined

Artur79
  • 12,705
  • 1
  • 22
  • 22

1 Answers1

1

I'v managed to make it working with Promises. But I'm still eager to know how to rewrite it using async/promise.

  const userAllowedToDeployWithPromise = (user_id, project, env) => {
    return new Promise(function(resolve, reject) {
      controller.storage.users.get(user_id, function(err, stored_user) {
        let result = (some calculations from stored_user here);
        resolve(result);
      });
    });
  };

  controller.hears(['^deploy (\\w+) (\\w+)'], 'direct_mention, mention', function(bot, message) {
    let channel = message.channel;
    let project = message.match[1];

    let allowed_promise = userAllowedToDeployWithPromise(message.user, project);

    allowed_promise.then(function(allowed) {
      if(allowed) {
        deployProject(project);
      } else {
        // some message here
      }
    });
Artur79
  • 12,705
  • 1
  • 22
  • 22
  • Did you ever figure this out? I'm having the same issue with a long form conversation I'm trying to implement with botkit. – Yaron Idan Jun 09 '19 at 18:05