javascript - 如何在 then() 中为 Bluebird 回调传递参数?

标签 javascript node.js promise bluebird

我有一个返回 promise 的电话。此时此刻,我这样做:

Something( ... )
  .then(()=>{console.log("Done.");});

这样会更实用:

Something( ... )
  .then(console.log, "Done.");

例如,setTimeout 的工作方式如下:

setTimeout(console.log, 1000, "Done.");

Bluebird有什么方法吗?我的目标是使用这个实用选项来减少 Promises 生成的已经荒谬的代码量。

最佳答案

At this moment, I do this:

Something(…).then(()=>{console.log("Done.");});

这是正确的做法。箭头函数已经大大缩短了它。请注意,您可以删除 "{"...";}"部分。

This would be more practical:

Something(…).then(console.log, "Done.");

不,不会。 then 的第二个参数是onRejected 回调,不是字符串。你不能那样做。

My aim is to reduce the already ridiculous amount of code that Promises generate.

然后使用async/await 语法和一个转译器。就这么简单

await Something(…);
console.log("Done");

Does Bluebird have any method for this?

如果你不喜欢使用转译器但是在 ES6 环境中(比如最近的 Node.js),你可以使用生成器函数来模仿 async/awaitPromise.coroutine .

关于javascript - 如何在 then() 中为 Bluebird 回调传递参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37548620/

相关文章:

javascript - 我怎样才能看到代表应用了悬停的栏的当前元素

javascript - 在每行达到特定字符限制时在文本编辑器中自动输入

javascript - JQuery(也许是 javascript)澄清

javascript - 在 Internet Explorer 中实现 Mozilla 的 toSource() 方法

for 循环中的 Promise

javascript - 使用 Promise.all 解决获取请求

javascript - React 和 FLUX - 我将在 Web 应用程序中的哪里实现服务器例程?

javascript - 如何使用 NodeJS Express 服务器在 HTML 中包含 JS 文件?

node.js - 停止 webpack 在文件底部添加 script 标签

javascript - "Promisifying"缓存响应