【发布时间】:2018-06-26 05:19:21
【问题描述】:
您好,我是异步编程的新手。我无法理解如果承诺成功解决,代码是否仍然是异步的。 例如: 模块 A 具有返回承诺的函数 A()。 我需要模块 B 中的模块 A 并调用函数 A() 模块 B 中的代码如下所示:
Section X: some code in module B
Section Y: ModuleA.functionA().then((result) => {
somevariableInModuleB = result;
// assign the result from A() to some variable in module B.
// some more logic goes here....
});
Section Z: some more code in module B....
那么,这段代码是否同步执行,即先是 X 部分,然后是 Y 部分,然后是 Z 部分? 还是我必须像这样修改它:
Section X: some code in module B
Section Y: ModuleA.functionA().then((result) => {somevariableInModuleB = result;})
Section Z: .then(() => {some more code in module B....});
这能保证吗?
【问题讨论】:
-
其中一些代码在另一个模块中的事实是完全不相关的;你想用
result做的所有事情都必须在.then()内部发生。
标签: javascript node.js promise es6-promise