Promise 的 then 处理程序中的同步代码 [英] Synchronous code in then handler of Promise
问题描述
在异步 Promise 解决后,我需要执行一些同步代码.目前我的代码如下所示:
I need to execute some synchronous code after an asynchrnous Promise is resolved. Currently my code looks like this:
console.log("Starting");
promiseFunc().
then(function() {
console.log("PromiseFunc done. Doing Synchronous work...");
synchronousFunc1();
synchronousFunc2();
console.log("Synchronous work done");
}).
catch(function(err) {
console.log("Error:" + err);
}).
then(function() {
console.log("Done")
});
一切正常:第一个 then
在 promiseFunc
解析后执行,最后的 then
最后执行.我认为规范希望我从 then
返回一个新的 Promise 或一个值,但我无法返回任何有用的东西.
以下是我的问题:
Everything is working fine: The first then
is executed after promiseFunc
is resolved and the final then
is executed last.
I think the spec expects me to return a new Promise or a value from the then
, but there is nothing useful I can return.
Here are my questions:
- 在承诺解决后执行同步代码的这种模式"有意义吗?从
then
块返回 undefined 可以吗? - 如果有人决定使用承诺实现
synchronousFunc1
会发生什么?这将破坏执行顺序.我认为不应该做出如此重大的改变.相反,应该实现另一个asynchronousFunc1
.我说得对吗? - 我的
catch/then
实现怎么样?最后的then
应该总是被执行.如果在catch
处理程序中抛出异常,我认为这将不起作用.有替代品吗?我知道 bluebird 中有一个finally
处理程序,但我只想使用标准的 Promise 功能.
- Does this 'pattern' for executing synchronous code after a promise is resolved make sense? Is it okay to return undefined from the
then
block? - What happens if someone decides to implement
synchronousFunc1
with a promise? This will break the order of execution. I think such a breaking change shouldn't be made. Instead anotherasynchronousFunc1
should be implemented. Am I right? - What about my
catch/then
implementation? The finalthen
should be executed always. I think this will not work, if an exception is thrown in thecatch
handler. Are there alternatives? I know there is afinally
handler in bluebird, but I want to use standard Promise features only.
推荐答案
在承诺被解析后执行同步代码的这种模式"有意义吗?
Does this 'pattern' for executing synchronous code after a promise is resolved make sense?
是的.这就是我们所做的.
Yes. That's what we all do.
可以从 then
块返回 undefined
吗?
Is it okay to return
undefined
from thethen
block?
并非如此,每项工作都应该有结果(否则将毫无意义),您应该返回
那个.但是如果你的函数只是执行副作用,那么返回 undefined
就是结果,所以没关系.
Not really, every work should have a result (otherwise it would be pointless) and you should return
that. But if your function is only executing side effects, then returning undefined
is the result, so it's okay.
如果有人决定使用承诺实现 synchronousFunc1
会发生什么?这会破坏执行顺序.
What happens if someone decides to implement
synchronousFunc1
with a promise? This will break the order of execution.
是的,这是一个突破性的变化.
Yes, that's a breaking change.
我认为不应该做出如此重大的改变.相反,应该实现另一个 asynchronousFunc1
.我说得对吗?
I think such a breaking change shouldn't be made. Instead another
asynchronousFunc1
should be implemented. Am I right?
如何处理重大更改取决于库的政策及其与调用者的合同.它可能是一个主要的版本更新,可能是一个新功能,可能是任何东西;当然应该适当地宣布.无论如何,您都必须更改代码.
How breaking changes are handled depends on the policy of the library and its contracts with callers. It might be a major version update, it might be a new function, it might be anything; it should be announced properly of course. You'll have to change your code anyway.
如果函数名暗示它是同步的,它不应该返回一个promise;在这种情况下,名称应该随实现一起更改.
If the name of the function suggests that it's synchronous, it shouldn't return a promise; the name should be changed with the implementation in that case.
我的 catch/then
实现怎么样?最后的 then
应该总是被执行.如果在 catch
处理程序中抛出异常,我认为这将不起作用.
What about my
catch/then
implementation? The finalthen
should be executed always. I think this will not work, if an exception is thrown in thecatch
handler.
正确.您必须确保 catch
处理程序不会抛出 :-)
Correct. You'll have to make sure that the catch
handler doesn't throw :-)
有其他选择吗?我知道 bluebird 中有一个 finally
处理程序,但我只想使用标准的 Promise 功能.
Are there alternatives? I know there is a
finally
handler in bluebird, but I want to use standard Promise features only.
如果你使用 bluebird,你应该使用 finally
.无论如何,它可能会变得标准化,并且许多其他库也支持它.如果没有,您可以随时自己填充.它的实现优于您的实现,尤其是在返回值和适当处理取消等方面.
If you are using bluebird, you should use finally
. It'll likely become standardised anyway, and many other libs support it as well. If not, you can always shim it yourself. Its implementation is superior to yours, especially about return values and handling things like cancellation appropriately.
这篇关于Promise 的 then 处理程序中的同步代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!