热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

kriskowal/qnode.jsq.all和spread-kriskowal/qnode.jsq.allandspread

Ihaveafunctionthatneedstheresultofthreepriorpromisespassedtoit.Oneislinearlydepend

I have a function that needs the result of three prior promises passed to it. One is linearly dependent and the other two can run concurrently. I want to use q.all to resolve the three promises, and then pass the results onto the fourth using .spread. My code however does not work. Any help would be appreciated.

我有一个函数需要传递给它的三个先前的承诺的结果。一个是线性相关的,另外两个可以同时运行。我想使用q.all解析三个promise,然后使用.spread将结果传递给第四个。但我的代码不起作用。任何帮助,将不胜感激。

var p1 = doWork(data);
var p2 = p1.then(doMoreWork);
var p3 = doConcurrentWork(data);

return q.all([p1,p2,p3]).spread(funcWith3params)
        .fail(function(err) {
            console.log(err):
        }

I can trace the code in node-inspector and see that the first 3 promises are being called. However, the function that .spread calls is not being called. Any clues as to why? Also .fail isn't being hit either.

我可以在node-inspector中跟踪代码,看看是否正在调用前3个promise。但是,.spread调用的函数没有被调用。任何线索为什么?另外.fail也没被击中。

2 个解决方案

#1


7  

Spread calls q.all internally.

在内部传播q.all。

Here is the code for spread from q.js:

这是从q.js传播的代码:

Q.spread = spread;
function spread(promise, fulfilled, rejected) {
    return when(promise, function (valuesOrPromises) {
        return all(valuesOrPromises).then(function (values) {
            return fulfilled.apply(void 0, values);
        }, rejected);
    }, rejected);
}

Note that it expects a promise that resolves to an array or an array as the first argument.

请注意,它期望一个解析为数组或数组的promise作为第一个参数。

Therefore your call should look like this:

因此,您的通话应如下所示:

var p1 = doWork(data);
var p2 = p1.then(doMoreWork);
var p3 = doConcurrentWork(data);

return q.spread([p1,p2,p3], funcWith3params, function(err) {
        console.log(err):
    });

However, your original call should work as well. Not sure why it does not.

但是,您的原始呼叫也应该有效。不知道为什么不这样做。

#2


3  

The most succinct way to express the example you’ve provided is:

表达您提供的示例的最简洁方式是:

var p1 = doWork(data);
var p2 = p1.then(doMoreWork);
var p3 = doConcurrentWork(data);

return Q.spread([p1, p2, p3], funcWith3params)
    .done();

However, the original is correct as written.

但是,原件正如所写的那样。

I suspect that the problem is that one or more of the input promises is never resolving. Try using timeouts to isolate the problem.

我怀疑问题是一个或多个输入承诺永远不会解决。尝试使用超时来隔离问题。

var p1 = doWork(data).timeout(1000, 'p1 timed out');
var p2 = p1.then(doMoreWork).timeout(1000, 'p2 timed out');
var p3 = doConcurrentWork(data).timeout(1000, 'p3 timed out');

return Q.spread([p1, p2, p3], funcWith3params)
   .done();

推荐阅读
author-avatar
波波微博1987_704
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有