【问题标题】:Function only returns on events函数只返回事件
【发布时间】:2016-09-25 06:31:31
【问题描述】:

我用console.log(openDialog()) 和openDialog() 调用一个函数,创建一个模态窗口,该函数只在某些事件上返回。

我的openDialog() 是这样的

function openDialog() {
  // do some html

  //
  buttonElement.onclick = function () {
    return 'some value';
  }
}

问题是当我调用console.log(openDialog())时,它会自动发现我的函数没有返回值,所以它只返回undefined。我希望它等到我的函数 openDialog() 返回一些东西。

也许是承诺?

编辑

有回调就这样吗?

function openDialog(cb) {
  // do some html

  buttonElement.onclick = function () {
    return cb('some value');
  }
}

console.log(openDialog(function (value) {
  return value;
});

【问题讨论】:

  • 你这样做的方式是让你的控制台语句在事件的回调中(onClick)。我假设您想在您的console.log 上打印'some value'?
  • 函数是同步的,你不能对它们使用promise。为什么需要openDialog() 才能返回值?
  • 除了openDialog() 将立即返回的主要问题之外,浏览器使用点击处理程序的返回值来决定是否取消点击的默认行为,因此该部分不会也没有意义。
  • 我正在尝试制作一个自定义的prompt(),所以我想显示对话框并获取用户在对话框中写入的内容,这也是事件仅在函数内部有效的原因。
  • 您需要重构代码以使用回调。也就是说,让openDialog() 接受一个回调函数作为参数,然后在您的点击处理程序中调用该回调函数来执行您想做的任何事情。

标签: javascript function events dom-events


【解决方案1】:

因此,假设您尝试创建自定义 prompt(),您必须记住,我们无法同步。

这使用了同步的原生prompt函数:

var result = prompt("What's your name");

你不能创建这样的函数。相反,您需要使其异步:使用回调(或承诺)。

简单的回调接口

function openDialog(buttonElement, cb) {
  buttonElement.onclick = function () {
    cb('some value');
  }
}

// Open the dialog
openDialog(document.querySelector("button"), function (result) {
  // After getting the result, this will be called
  alert(result);  
});
<button>Submit</button>

Promise 接口

function openDialog(buttonElement) {
  var resolve = null;
  var promise = new Promise(function (_resolve) {
    resolve = _resolve;
  });
  buttonElement.onclick = function () {
    resolve('some value');
  }
  return promise;
}

// Open the dialog
openDialog(document.querySelector("button")).then(function (result) {
  // After getting the result, this will be called
  alert(result);  
});
<button>Submit</button>

如果您也想在回调中发送错误,您只需调用callback 函数并将错误作为第一个参数。在 Promises 的情况下,使用 reject 函数:

function openDialog(buttonElement) {
  var resolve = null;
  var reject = null;
  var promise = new Promise(function (_resolve, _reject) {
    resolve = _resolve;
    reject = _reject;
  });
  buttonElement.onclick = function () {
    var name = your_name.value;
    if (!name) {
      return reject(new Error("Please enter a name in the input."));
    }
    resolve(name);
  }
  return promise;
}

// Open the dialog
openDialog(document.querySelector("button")).then(function (result) {
  // After getting the result, this will be called
  alert(result);  
}).catch(function (err) {
  alert(err.message);
});
<input id="your_name" />
<button>Submit</button>

对于回调情况,只需调用cb(null, name) 表示成功,cb(new Error("Please enter a valid name")) 表示错误。然后你会这样称呼它:

openDialog(document.querySelector("button"), function (err, result) {
  if (err) { return alert(err.message); }
  // After getting the result, this will be called
  alert(result);  
});

【讨论】:

  • 太棒了。谢谢!我见过很多function (err, result) { 或.then(result =&gt; ...).catch(err =&gt; ...) 的代码示例。我将如何构造代码,以便它也可以返回错误?
  • @Jamgreen 不客气!我也为此添加了一个示例。 :)
猜你喜欢
  • 2019-12-13
  • 1970-01-01
  • 2014-01-03
  • 2015-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-20
相关资源
最近更新 更多