问题标签 [bluebird]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
1555 浏览

javascript - Promise 和不定期的回调

我正在使用bluebird玩一个承诺控制流。Bluebird 提供了一个 .promisify() 方法,用于将常规回调函数转换为 promise 函数,但我不清楚当函数不规则时我应该做什么。例如requestjs请求的方法签名是

回调在哪里

而不是常规的

我应该如何将其转换为承诺?

0 投票
1 回答
15532 浏览

javascript - 与 fs 和 bluebird 的承诺

我目前正在学习如何在 nodejs 中使用 Promise

所以我的第一个挑战是列出目录中的文件,然后使用异步函数通过两个步骤获取每个文件的内容。我想出了以下解决方案,但有一种强烈的感觉,这不是最优雅的方法,尤其是我将异步方法“转换”为 Promise 的第一部分

现在链接两个承诺:

正如我在上面所写的,它返回所需的结果,但我很确定有一种更优雅的方法。

0 投票
2 回答
6736 浏览

node.js - 如何从嵌套的 Promise 中返回值?

我在 Redis 中有一组电影 ID: [1,2,3,4]和一组带有实际数据的哈希值。现在,我想一次性获取 ID 的所有电影数据。

我正在尝试使用蓝鸟承诺,但我被卡住了。到目前为止,我有:

问题在于我的尝试,我总是得到一个新的承诺,而在所有操作完成后我只对 JSON 感兴趣。

这里的任何人都可以对此有所了解吗?

0 投票
1 回答
858 浏览

javascript - yielding a mapped array of promise doesn't return anything

I am using the promise library bluebird with node 0.11.9 running with the flag --harmony

I have a simple working example:

#xA;

This works fine and output the list of nodes' stats. But with the following version: there is no output at all:

#xA;

Any idea of why this doesn't work ?

0 投票
2 回答
2298 浏览

javascript - 并行承诺的流畅构造

我的问题是当您需要将上下文和参数传递给构建承诺的函数时, BlueBird中的承诺的优雅并行化。

为了使我的问题易于理解和测试,我做了一个不依赖的例子。

假设我进行涉及异步“计算机”(必须释放其资源)的计算( 1/(x x x) + 1/(x*x) )。正方形和立方体是异步独立计算的。

我可以这样计算:

但与理论上可能的情况相比,我发现这种用法all太重了。当您将函数作为参数传递给 时then,它会被执行。当您将函数传递给 时all,它们不是,存在错误。我怀疑我缺少实用程序或模式...

有没有一种解决方案可以将其更改为这种风格的更简单的东西:

?

我可能会破解Promise.prototype.all或定义一个新函数以避免增加多态性,但我只对不涉及修改我不拥有的对象的解决方案感兴趣。


附件:

以下是我的测试如何定义“计算机”:

0 投票
6 回答
24696 浏览

javascript - Synchronous promise resolution (bluebird vs. jQuery)

I have developed a small lib for the Dynamics CRM REST/ODATA webservice (CrmRestKit). The lib dependes on jQuery and utilizes the promise-pattern, repectivly the promise-like-pattern of jQuery.

Now I like to port this lib to bluebird and remove the jQuery dependency. But I am facing a problem because bluebird does not support the synchronous resolution of promise-objects.

Some context information:

The API of the CrmRestKit excepts an optional parameter that defines if the web-service call should be performed in sync or async mode:

When you pass "true" or omit the last parameter, will the method created the record in sync. mode.

Sometimes it is necessary to perform a operation in sync-mode, for instance you can write JavaScript code for Dynamics CRM that is involed for the save-event of an form and in this event-handler you need to perform sync-operation for validation (e.g. validate that a certain number of child-records exist, in case the right number of records exist, cancel the save-operation and show an error message).

My problem now is the following: bluebird does not support the resolution in sync-mode. For instance when I do the following, the "then" handler is invoked in async fashion:

The output is the following:

I would expect that the "second" appears after the "first" because the promise is already resolved with an value. So I would assume that an then-event-handler is immediately invoked when applied on an already resolved promise-object.

When I do the same (use then on an already resolved promise) with jQuery I will have my expected result:

This will generate the following output:

Is there a way to make bluebird work in the same fashion?

Update: The provided code was just to illustrate the problem. The idea of the lib is: Regardless of the execution-mode (sync, async) the caller will always deal with an promise-object.

Regarding "... asking the user... doesn't seems to make any sense": When you provide two methods "CreateAsync" and "CreateSync" it is also up to the user to decide how the operation is executed.

Anyway with the current implementation the default behavior (last parameter is optional) is a async execution. So 99% of the code requires a promise-object, the optional parameter is only use for the 1% cases where you simply need a sync execution. Furthermore I developed to lib for myself and I use in 99,9999% of the case the async mode but I thought it is nice to have the option to go the sync-road as you like.

But I thinks I got the point an sync method should simply return the value. For the next release (3.0) I will implement "CreateSync" and "CreateAsync".

Thanks for your input.

Update-2 My intension for the optional parameter was to ensure a consistend behavior AND prevent logic error. Assume your as a consumer of my methode "GetCurrentUserRoles" that uses lib. So the method will alway return an promise, that means you have to use the "then" method to execute code that depends on the result. So when some writes code like this, I agree it is totally wrong:

I agree that this code will break when the method "GetCurrentUserRoles" changes from sync to async.

But I understand that this I not a good design, because the consumer should now that he deals with an async method.

0 投票
2 回答
2581 浏览

javascript - Node 中的 Bluebird.js 和异步 api 调用

所以我正在尝试使用 Facebook 集成构建我的第一个 web 应用程序(使用facebook-node-sdk)。我让它对 api 进行简单的调用,但现在是时候将这一切放在一个简单的服务器中并根据请求进行调用(这不会是 webapp 本身,而是更多的 API 服务器)。

我遇到的问题是,即使我(大概)使用bluebird来承诺 Facebook sdk 和我的makeCall方法,我仍然打印“hi”然后“undefined” -console.log在 makeCall 之前被调用可以返回任何东西。

这是我的app.js

0 投票
2 回答
2486 浏览

javascript - 蓝鸟承诺多个论点

我是 Promises 的新手,不知道如何解决这个问题:我正在做一个身份验证系统,我的第一个电话是检查数据库上的电子邮件。如果用户存在,则根据 bcrypted 密码检查密码......我正在使用这个 lib 进行 bcrypt:https ://npmjs.org/package/bcrypt这与承诺不兼容,所以我使用“promisify”以下签名:比较(密码,crypted_pa​​ssword,回调)。

所以这是我的代码:

这是我的 findByEmail 方法:

}

在这种情况下如何为“比较”方法分配多个值?我错过了承诺的意义吗?

0 投票
3 回答
36088 浏览

javascript - 蓝鸟,承诺,然后()

我只用了几天的蓝鸟,但我想回顾一下我所有的旧代码和promisify它:)

我的问题是我仍然没有完全掌握then()命令的流程。

考虑这两个块:

一个

  1. 在场景 A 中task3会得到task2? 在 B 中他们都得到了第一个承诺的结果?

  2. 第二个与Promise.all从蓝鸟跑步有什么不同?

  3. Promise.all在使用方法时,这些 A/B/有何不同catch(我把它放在哪里)。

抱歉,这是一堆问题。

0 投票
1 回答
3914 浏览

node.js - 等待所有承诺在 nodejs 中用 bluebird 完成

用蓝鸟等待所有承诺在nodejs中完成的最佳方法是什么?假设我想从数据库中选择记录并将它们存储在 redis 中。我想出了这个

不确定它是否按我的预期工作。所有条目都在 redis 中,但 console.log 显示空数组。它不应该包含一个'OK'数组,因为它是redis在履行承诺后返回的消息?我在这里想念什么?