蓝鸟承诺加入行为 [英] Bluebird Promises Join behaviour

查看:79
本文介绍了蓝鸟承诺加入行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用Node.js执行以下代码

  var Promise = require('bluebird'); 

Promise.join(
函数A(){console.log(A);},
函数B(){console.log(B); }
).done(
function done(){console.log(done);}
);

控制台将记录

  B 
完成

但我希望

  A 
B

  B 
A

如果在功能A中设置断点,则永远不会达到。为什么它处理B而不是A?

解决方案

Promise.join promises 作为其所有参数,但是它的最后一个参数,这是一个函数。

 承诺.join(Promise.delay(100),request(http://...com),function(_,res){
//已经过了100毫秒并且请求已经返回。
});

您正在为它提供两个功能,因此它执行以下操作:




  • 承诺超过函数A(){...} - 基本上返回对它的承诺

  • 完成后(立即)执行最后一个参数,函数B(){...} 记录它。



查看文档:


Promise.join(Promise | Thenable | value promises ...,Function handler) - >承诺



用于协调多个并发离散承诺。虽然.all()适用于处理动态大小的统一承诺列表,但是当你有一些固定数量的离散承诺要同时协调时,Promise.join更容易使用(并且性能更高),例如: / p>

var Promise = require(bluebird);



var join = Promise.join;



join(getPictures(),getComments (),getTweets(),



      function(pictures,comments ,推文){



      console.log(总计: + pictures.length + comments.length + tweets.length);



});







更新:



JSRishe提出了另一种巧妙的方法来解决这种模式在这个答案中看起来像:

  Promise.delay(100).return(request(http://...com).then(function(res){
//已经过了100毫秒,请求已返回
});

这是有效的,因为请求已经在延迟返回之前发生,因为在同一范围内调用该函数。


If I execute the following code with Node.js

var Promise = require('bluebird');

Promise.join(
    function A() { console.log("A"); },
    function B() { console.log("B"); }
).done(
    function done() { console.log("done");}
);

The console will log

B
done

However I would expect

A
B
done

or

B
A
done

If it set a break point in function A it is never reached. Why is it that it processes B but not A?

解决方案

Promise.join takes promises as all its arguments but its last one, which is a function.

Promise.join(Promise.delay(100), request("http://...com"), function(_, res){
       // 100 ms have passed and the request has returned.
});

You're feeding it two functions so it does the following:

  • Make a promise over function A() { ... } - basically returning a promise over it
  • When it's done (immediately) execute the last argument, function B() {... } logging it.

See the docs:

Promise.join(Promise|Thenable|value promises..., Function handler) -> Promise

For coordinating multiple concurrent discrete promises. While .all() is good for handling a dynamically sized list of uniform promises, Promise.join is much easier (and more performant) to use when you have a fixed amount of discrete promises that you want to coordinate concurrently, for example:

var Promise = require("bluebird");

var join = Promise.join;

join(getPictures(), getComments(), getTweets(),

     function(pictures, comments, tweets) {

     console.log("in total: " + pictures.length + comments.length + tweets.length);

});


Update:

JSRishe came up with another clever way to solve this sort of pattern in this answer which looks something like:

Promise.delay(100).return(request("http://...com").then(function(res){
    // 100 ms have passed and the request has returned 
});

This works because the request already happens before the delay returns since the function is called in the same scope.

这篇关于蓝鸟承诺加入行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆