- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在使用 Inquirer使用 Node.js 的库,在使用 promise 时我仍然会遇到厄运金字塔,我做错了什么?
仅供引用,查询器库 API 基本上是:
inquirer.prompt([
question1,
question2,
question3,
...
questionX
]).then(function(answers){});
其中 answers 是一个散列,带有代表每个问题的键。这里没有什么特别的地方。
无论如何,使用 API,我总是得到 getAnswersToPrompts().then(function(answers){})
并且将 promise 嵌套在前一个中似乎更方便......比如所以:
function run (rootDir) {
return watchHelper().then(function (answers) {
return chooseDirs({
allowDirs: answers.allow,
originalRootDir: rootDir,
onlyOneFile: false
}).then(function (pathsToRun) {
assert(pathsToRun.length > 0, ' You need to select at least one path.');
return getOptions(availableOptionsForPlainNode).then(function (answers) {
const selectedOpts = answers[ 'command-line-options' ];
return localOrGlobal().then(function (answers) {
const sumanExec = answers.localOrGlobal;
console.log(' => ', colors.magenta.bold([ '$', sumanExec, '--watch', pathsToRun, selectedOpts ].join(' ')));
});
});
});
}).catch(rejectionHandler);
}
我可能会这样做:
function run(){
return makePromise()
.then(fn1(data1))
.then(fn2(data2))
.then(fn3(data3))
}
fn1、fn2、fn3 看起来像:
function fnX(data){
return function(answers){
return promise(data);
}
}
但这只会让理解 AFAICT 变得更加复杂
尽可能清楚,我确实需要之前 promise 的结果,但有时我需要之前 promise 的结果,甚至需要之前的结果。
由于闭包等,嵌套函数允许我需要的数据在范围内。
最佳答案
返回下一个 Promise before 调用 then
:
function run (rootDir) {
var pathsToRun;
return watchHelper()
.then(function (watchHelperAnswers) {
return chooseDirs({
allowDirs: watchHelperAnswers.allow,
originalRootDir: rootDir,
onlyOneFile: false
});
}).then(function (chooseDirsResult) {
assert(chooseDirsResult.length > 0, ' You need to select at least one path.');
pathsToRun = chooseDirsResult;
return getOptions(availableOptionsForPlainNode);
}).then(function (getOptionsAnswers) {
const selectedOpts = getOptionsAnswers[ 'command-line-options' ];
return localOrGlobal();
}).then(function (localOrGlobalAnswers) {
const sumanExec = localOrGlobalAnswers.localOrGlobal;
console.log(' => ', colors.magenta.bold([ '$', sumanExec, '--watch', pathsToRun,
selectedOpts ].join(' ')));
}).catch(rejectionHandler);
}
but sometimes I need the result from the promise prior to that or even the result prior to that
您的示例中的唯一实例是 pathsToRun
。我认为嵌套函数两三个深度以容纳它仍然是可读的,但您的另一个选择是在 promise 链之外定义一个变量,我在上面为 pathsToRun
显示了这一点。
最后,您的示例在整个 promise 链中使用了三个不同的变量,它们都称为 answers
,这可能会增加困惑。总的来说,我认为对 promise 回调结果使用相同的名称是可以的,但为了清楚起见,我在这里重命名了它们。
关于javascript - 使用 promise 时我仍然会遇到厄运金字塔,我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40569825/
我想简化/减少我的代码,所以我尝试将具有重复参数的类的初始化放在它们自己的扩展类中。这是一个基于 Pyramid & Cornice 的 REST API。 我如何初始化 pyramid.httpex
我正在尝试构建一个由 4 个 DIV 组成的金字塔。布局如下所示: ------ | #1 | ------ ---------------- | #2 | #3 | #
我是 3d 绘图的新手。我只想用 5 个点 build 一个金字塔,然后从中切出一个平面。我的问题是我不知道如何填充边。 points = np.array([[-1, -1, -1],
我需要创建金字塔 slider 。 正如您在我当前的实现中看到的那样,我需要有一个位于其余元素顶部的中心图像。它们形成金字塔结构。图像也被放置在彼此下面,这种效果打破了一切,因为我需要转换它们(中心图
我一直在玩我在这里找到的演示:http://codepen.io/singhiskng/pen/dqiGj 我正在尝试制作一个 4 边形金字塔。
在jsp中实现如图所示的金字塔, 代码如下: 大致思路如下: 1、先画出前5行。因为 i+空格数=总行数,所以第一行的空格数是 总行数-i(第几行),然后画出 *,*的数目=2*i-11;
我在尝试找出一种使用用户输入创建金字塔的方法时遇到了很大的麻烦。它应该是这样的。 Enter a number between 1 and 9: 4 O O O O OOOO OOOO OOOO OO
我一直在寻找解决厄运金字塔的办法。到目前为止,我得出的结论如下: router.use('/create', function(res,req,next) { try { v
我刚刚开始处理。因此,我想创建一个函数,允许任何用户输入边数来创建 3D 金字塔。我正在使用处理 3。我需要一些帮助。提前致谢。我编写了一段目前有效的代码,但我想概括它。任何建议/提示将不胜感激。提前
我使用 JavaScript 创建了一个金字塔以下是我迄今为止使用 for 循环尝试的代码: function showPyramid() { var rows = 5; var ou
我被告知数据库超时(连接、交互、等待)设置为 30 秒。该数据库上有一个网站,人们往往会闲置超过 30 秒。 使用sqlalchemy,我需要在继续查询之前检查mysql连接是否仍然有效,否则站点会出
另一个棘手的问题。你在这里看到的是我的物理金字塔,它由 3 个 LED 构成,在 1 个平面上形成一个三角形,另一个 LED 在中间中心,比其他 3 个高约 18 毫米。第 4 个使三角形成为金字塔。
目标: 我正在尝试生成类似于下面给出的格式的金字塔。这需要一个基本的 Java 程序来接受用户输入、将数字转换为字符串、使用嵌套循环并生成格式化输出。下面是使用 8 行的所需输出示例。 Enter t
我的数据是多维组合数据(所有维度总和为 1 或 100)。我已经学会了如何使用三个变量来创建二维三元图。 我想添加第四个维度,使我的情节看起来像这样。 我愿意使用 python 或 R。我现在正在使用
我的数据是多维组合数据(所有维度总和为 1 或 100)。我已经学会了如何使用三个变量来创建二维三元图。 我想添加第四个维度,使我的情节看起来像这样。 我愿意使用 python 或 R。我现在正在使用
我是一名优秀的程序员,十分优秀!