【发布时间】:2021-05-05 01:20:58
【问题描述】:
在 JS 中,我想:
- 定义一个定义承诺的函数。
- 这个函数然后有一个事件来解决这个承诺。
- 该函数返回未解决的承诺。
- 当 promise 被解析时,调用者将收到解析的值。
类似这样的:
function my_func() {
// This creates an empty promise.
const p = Promise;
// When a user clicks a button in UI then we resolve the promise.
// This would be in an event!
// p.resolve('user clicked OK');
// Returns empty promise.
return p;
}
function main() {
// Here when the promise is resolved then we get the value.
my_func().then(function(v) {
console.log(v);
});
}
【问题讨论】:
-
就这样吧?
return new Promise(resolve => { okButton.onclick = (e) => resolve('user clicked ok'); })
标签: javascript function promise