从TypeScript中的回调返回值

时间:2019-10-23 07:33:07

标签: javascript node.js typescript callback

我在Service.ts文件的typecrypt中有一个函数:

export const doCallAuth = (username, password) => {
    var auth = new Auth({
        url: '...',

      });

      var status;

      auth.authenticate(username, password, function (err, user) {
        if (err) {
          console.log(err);
          status = 'no';

        } else if (!user.uid) {
          console.log("user not found Error");
          status = 'no';
        } else if (user.uid) {
          console.log("success : user " + user.uid + " found ");
          status = 'yes';
        }
      });

      return status;
}

我通过以下方式调用此方法:

var result = Service.doCallAuth('test', 'test');

变量结果未定义

我不知道为什么结果未定义

有人可以帮我吗?

先谢谢了。 :)

3 个答案:

答案 0 :(得分:0)

尝试这个。

 export const doCallAuth = (username, password, callback) => {
        var auth = new Auth({
            url: '...',
        });
        var status;
        auth.authenticate(username, password, callback);
    }
    var result;
    Service.doCallAuth('test', 'test', function (err, user) {
        if (err) {
            console.log(err);
            status = 'no';
        } else if (!user.uid) {
            console.log("user not found Error");
            status = 'no';
        } else if (user.uid) {
            console.log("success : user " + user.uid + " found ");
            status = 'yes';
        }
        result = status;
    });

答案 1 :(得分:0)

要从AJAX调用中获取价值,需要使用asyncawait

    export const doCallAuth = async function(username, password) => {
        var auth = new Auth({
            url: '...',

          });

          var status;

          auth.authenticate(username, password, function (err, user) {
            if (err) {
              console.log(err);
              status = 'no';

            } else if (!user.uid) {
              console.log("user not found Error");
              status = 'no';
            } else if (user.uid) {
              console.log("success : user " + user.uid + " found ");
              status = 'yes';
            }
            return status;
          });


    }

通过以下方式调用此方法:

var result = await  Service.doCallAuth('test', 'test');

答案 2 :(得分:0)

您可以使用Promise或回调模式

1。承诺

export const doCallAuth = (username, password) => {
  return new Promise((resolve, reject) => {
    var auth = new Auth({ url: "..." });
    auth.authenticate(username, password, (err, user) => {
      if (err) {
        reject(err);
      } else if (!user.uid) {
        reject(new Error("user not found Error"));
      } else if (user.uid) {
        console.log("success : user " + user.uid + " found ");
        resolve(user);
      }
    });
  });
};

Service.doCallAuth("test", "test")
  .then(user => {
    console.log("success : user " + user.uid + " found ");
  })
  .catch(e => {
    console.log(e);
  });

2。回调

export const doCallAuthCallBack = (username, password, callback) => {
  var auth = new Auth({ url: "..." });
  auth.authenticate(username, password, (err, user) => {
    if (err) {
      callback(err);
    } else if (!user.uid) {
      callback(new Error("user not found Error"));
    } else if (user.uid) {
      callback(null, user);
    }
  });
};

Service.doCallAuthCallBack("test", "test", (err, user) => {
  if (err) {
    console.log(err);
  } else {
    console.log("success : user " + user.uid + " found ");
  }
});