我有一个返回 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
/await
与Promise.coroutine
.
关于javascript - 如何在 then() 中为 Bluebird 回调传递参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37548620/