- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有以下代码:
someService.fnReturnsPromise()
.then(function () {
return someService.fnReturnsAnotherPromise(someArg);
})
.then(function (resultsOfSecondFn) {
// do stuff with results
});
我觉得这应该行得通;但是,resultsOfSecondFn
实际上并不是结果,它是我返回的 promise 本身。为了让它按照我想要的方式工作,我必须这样做:
someService.fnReturnsPromise()
.then(function () {
return someService.fnReturnsAnotherPromise(someArg);
})
.then(function (promiseReturn) {
promiseReturn.then(function (results) {
// do stuff with results
});
});
这是 fnReturnsAnotherPromise
的伪代码:
someService.fnReturnsAnotherPromise = function (arg1) {
return anotherService.anotherFnThatReturnsPromise(arg1);
};
实际上,它只是一个额外的层,但无论哪种方式都会返回一个 promise 。 anotherFnThatReturnsPromise
的代码是 $q.defer()
的简单范例,return dfd.promise
带有一些 resolve()
s.
最佳答案
像 Angular 这样的 Promise 是 Promises/A+ 兼容的,并且保证递归地吸收 promises。这正是为了避免嵌套和简化您的情况,并且是 promise 的重点。
因此,即使您有一个返回的 promise 和一个返回 promise 的 promise ,您也可以在单个 .then
调用中解包它。例如:
var p = $q.when(1); // Promise<Int>
var p2 = $q.when().then(function(){ return p; }); // Promise<Promise<Int>>
var p3 = $q.when().then(function(){ return p2; }); // Promise<Promise<Promise<Int>>>>
p3.then(function(result) {
console.log(result); // Logs 1, and Int and not p2's type
});
或者在你的例子中:
someService.fnReturnsPromise()
.then(function() {
return someService.fnReturnsAnotherPromise(someArg);
})
.then(function(resultsOfSecondFn) {
// do work with results, it is already unwrapped
});
参见 this comparison with another language对未展开的 promise 的看法。
关于javascript - Angular : How to flatten this Promise chain?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25512015/
torch.flatten() 和 torch.nn.Flatten() 有什么区别? 最佳答案 扁平化在 PyTorch 中以三种形式提供 作为张量方法(oop 风格) torch.Tensor.f
我有以下(不完整的)功能: /* Populates char* name with the named location of the ith (flat) element * of an arr
我正在尝试使用 Keras 和基于 Marcin 的 PS3 示例的 Tensorflow 后端构建一个可变长度序列分类模型:https://stackoverflow.com/a/42635571/
我开始使用 keras 和 tensorflow 进行深度学习。在最初阶段,我有一个疑问。当我使用 tf.contrib.layers.flatten (Api 1.8) 用于拼合图像(也可以是多 c
鉴于以下SVG: 是否有任何工具可以通过变换自动更新路径点,并从组元素中删除变换? 我有一个SVG应用程序(草图),它使用转换属性(如果您
我目前有一个 Python 脚本,它使用 flatten 函数来展平 JSON 对象,然后我将使用 json_normalize将扁平的 JSON 转换为 DataFrame。 import pand
我注意到Option.flatten是defined,如下所示: def flatten [B](隐式ev:A To) with Serializable ... implicit def conf
我希望有人经历过相同的过程并可以帮助我看看以下情况是否可能。 我目前构建了一个从 S3 存储桶进行复制的管道。该存储桶包含大量文件夹。 Azure 数据工厂是否有办法在从 S3 存储桶复制数据时忽略文
我希望有人经历过相同的过程并可以帮助我看看以下情况是否可能。 我目前构建了一个从 S3 存储桶进行复制的管道。该存储桶包含大量文件夹。 Azure 数据工厂是否有办法在从 S3 存储桶复制数据时忽略文
这个问题在这里已经有了答案: How to flatten tree via LINQ? (15 个答案) 关闭 6 年前。 我有一个嵌套类: class Item {
我有下表: group_id amount type 1 10 1 1 2 1 1 5 2 1
我有兴趣实现一个通用的 flatten一个容器的容器 为了简化,我会使用自己的容器。主要原因是标准容器接收分配器的附加模板参数,这对我编写操作来说更复杂。因此,考虑容器 Vector , List等让
给定如下列表(我的实际列表比这个长,列表中至少有100个子列表): [[135, "Apple Banana"], [136, "Peach Watermelon"], [137, "Orange S
这个问题在这里已经有了答案: Flatten an irregular (arbitrarily nested) list of lists (51 个答案) 关闭 5 年前。 我有一个不规则的数据
我创建了一个函数,它接受输入变量的向量,并为每组输入返回一个元胞数组。最终输出变量 (out) 似乎由一个包含两个 1x5 单元格的 2x1 单元格组成。我在下面提供了截图: 我只是想弄清楚如何将元胞
我正在尝试编写一个方法,它将接受一个由字符 0、1 和 ? 组成的字符串,并返回由原始字符串表示的一组字符串,将 ? 作为通配符,可以是 0 或 1。例如,如果输入是 "011?0",则输出应该是 [
假设我有一堆从 Pig UDF 生成的数据包,其中包含多个字符串元组。我怎样才能将它们全部从数据包中拉出来并简单地使每个字符串成为它自己的数据“行”。 databags = FOREACH 数据生成
所以我的目标是: fruitColourMapping = [{'apple': 'red'}, {'banana': 'yellow'}] 到 finalMap = {'apple': 'red',
我需要使用 Serialize 和 Deserialize 的自定义实现,但我不知道如何做 #[serde(flatten)] 有没有人知道? 注意:我知道我可以将较低元素的完整实现完全重写到较高元素
我有一个 FLATTEN LAMBDA 函数,可以将数组中的数据展平。这很好用,但我想集成另一个数组参数,这样我就可以使用不连续的范围。 在我的示例中,A1:B6 范围位于 array 中并返回展平数
我是一名优秀的程序员,十分优秀!