/**
* @param {(callback:any)=>any} f
*/
function unselfAsyncfunction(f) {
let canInvoke = true;
return (callback) => {
if (canInvoke) {
canInvoke = false;
f(function(arg) {
if (typeof callback ==='function') {
callback(arg);
}
canInvoke = true;
});
}
};
}
1
waiaan 2019-05-28 16:41:25 +08:00 1
没看懂问题。
|
2
TomVista OP |
3
IsaacYoung 2019-05-28 17:03:23 +08:00 1
|
4
Chrisssss 2019-05-28 17:08:53 +08:00 1
你说的是这样的吗?
```javascript function unselfAsyncfunction (fn) { let canInvoke = true return function () { if (!canInvoke) { canInvoke = true fn(...arguments).finally(() => { canInvoke = false }) } } } ``` |
5
noe132 2019-05-28 17:11:29 +08:00 1
|
6
johnnyNg 2019-05-28 17:12:13 +08:00 1
```javascript
const unselfAsyncfunction = async fn => { let isDone = true return async (...args) => { if (!isDone) { throw new Error('pre call is not down!') } isDone = false const rsp = await fn(...args) isDone = true return rsp } } ``` |
7
johnnyNg 2019-05-28 17:15:25 +08:00 1
楼主用的 callback 风格的异步,建议封装成 Promise 形式的异步
|
9
mooncakejs 2019-05-28 17:38:04 +08:00 1
@noe132 没有处理 catch,
|
10
TomVista OP 思路有了,我去试试.
|