问题描述
我使用一个名为setConf的函数进行测试.有时候,如果我可以使用它来启动一个Promise链,那真的很方便,有时我只需要它来返回值.
I use a function called setConf for testing. Sometimes it's really convenient if I can use it to start a promise chain, sometimes I just need need it to return the value.
是否可以将现有功能声明为异步功能?我想做的但不起作用的是:
Is it possible to declare an existing function as async? What I would like to do but it doesn't work is:
const setConf = (conf) => {
mainConf = Object.assign(mainConf , conf)
return mainConf
}
module.exports.asyncSetConf = async setConf
module.exports.setConf = setConf
我目前有这个
exports.asyncSetConf = async (conf) => {
mainConf = Object.assign(mainConf , conf)
return mainConf
}
exports.setConf = (conf) => {
mainConf = Object.assign(mainConf , conf)
return mainConf
}
代码相同,无法重构它似乎很愚蠢
The code is identical, It seems kinda silly not to be able to refactor this
推荐答案
首先,使用async
关键字声明的函数将返回promise.因此,您要么希望函数返回承诺,要么不希望返回.没有用async
关键字声明的函数那样的东西,它有时会返回promise,有时却不返回.
First off, a function declared with the async
keyword returns a promise. So you either want your function to return a promise or you don't. There is no such thing as a function that is declared with the async
keyword that sometimes returns a promise and sometimes doesn't.
第二,如果您的函数中没有异步代码,那么通常会使事情变得更复杂,将异步操作包装在异步接口中也变得不那么复杂,因此通常没有理由这样做.
Second off, if you have no asynchronous code in your function, then it's generally making things more complicated, not less complicated to wrap synchronous operations in an asynchronous interface, so there's generally no reason to do that.
第三,如果您只是想有时使用setConf()
来启动承诺链,则可以在需要时将其简单地用Promise.resolve()
包装:
Third off, if you just want to sometimes use setConf()
to start a promise chain, you can simply wrap it with Promise.resolve()
when you want to do that:
Promise.resolve(setConf(...)).then(f).then(g).catch(...)
当然,您也不必这样做,因为如果f()
是异步的并返回一个Promise,则可以执行以下操作:
Of course, you don't necessarily need to do that either because if f()
is asynchronous and returns a promise, you could just do this:
f(setConfg(...)).then(g).catch(...)
如果您发现自己经常使用Promise.resolve(setConf(...))
,则可以为其创建实用程序功能:
If you find yourself using Promise.resolve(setConf(...))
a lot, then you can just make a utility function for it:
function setConfPromise(...args) {
return Promise.resolve(setConf(..args))
}
您可以更改其声明,也可以将其包装到另一个函数中.您有时不能通过async
行为动态地实现它,有时则不能.为此,您需要两个单独的功能.
You can change its declaration or you can wrap it with another function. You can't dynamically make it sometimes with async
behavior and sometimes not. You need two separate functions for that.
您可以像这样包装它:
module.exports.asyncSetConf = function(...args) {
return Promise.resolve(setConf(...args));
}
module.exports.setConf = setConf
仅供参考,async
仅在实际需要时使用.有关其用法的一些注意事项:
FYI, async
should only be used when actually needed. Some notes about its usage:
- 当不涉及实际的异步代码时,切勿使用
async
.将异步接口放在同步代码上只会使功能的使用比所需的更为复杂. - 要在函数内部使用
await
时,请使用async
. - 当您强制自动返回承诺并让某些异常自动捕获并变成拒绝的承诺时,请使用
async
.
- You should never use
async
when there no actual asynchronous code involved. Putting an asynchronous interface on synchronous code just makes use of the function more complicated than required. - Use
async
when you want to useawait
inside the function. - Use
async
when you to force automatically returning a promise and have some exceptions automatically caught and turned into a rejected promise.
就我个人而言,我只想在函数内使用await
时会使用async
.
Personally, I'd only ever use async
when I want to use await
inside the function.
您无需仅使用async
即可返回承诺.您只需返回承诺即可做到老式".
You do not need to use async
just to return a promise. You can do that the "old-fashioned" way by just returning a promise.
使用async
不会使任何同步代码突然异步运行(常见的新手假设).它根本不会更改同步代码.它强制返回值是一个承诺,调用方必须使用await
或.then()
来获取返回值,从中将强制在.then()
上执行下一个滴答",但是如果函数全部是同步的,它仍将同步执行-async
不会更改它.
Using async
does not make any synchronous code suddenly run asynchronously (a common newbie assumption). It doesn't change synchronous code at all. It forces the return value to be a promise which the caller has to use either await
or .then()
with to get the value from which will force a "next tick" execution on the .then()
, but if the code in the function was all synchronous, it will still execute synchronously - async
doesn't change that.
这篇关于将现有功能声明为异步功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!