- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在寻找一种方法,使用 Node v7.6 或更高版本,以获得 Bluebird调用异步函数时的 promise (或任何非本地 promise )。
我可以这样做:
global.Promise = require('Bluebird'); // Or Q/When
var getResolvedPromise = () => Promise.resolve('value');
getResolvedPromise
.tap(...) // Bluebird method
.then(...);
参见:May I use global.Promise=require("bluebird")
我希望能够做类似的事情:
global.Promise = require('Bluebird'); // Or Q/When
var getResolvedAsyncAwaitPromise = async () => 'value';
getResolvedAsyncAwaitPromise()
.tap(...) // Error ! Native Promises does not have `.tap(...)`
.then(...);
我知道我可以随时使用类似的东西:
Bluebird.resolve(getResolvedAsyncAwaitPromise())
.tap(...);
但是我很好奇是否有办法改变 AsyncFunction
返回的默认 Promise。构造函数似乎是封闭的:
Note that AsyncFunction is not a global object. It could be obtained by evaluating the following code.
Object.getPrototypeOf(async function(){}).constructor
MDN reference on AsyncFunction
如果没有办法改变AsyncFunction
的Promise构造函数,我想知道这个锁的原因。
谢谢!
最佳答案
Is there a way to change the default Promise returned by AsyncFunction
没有。
What are the reasons of this locking
劫持所有异步函数
的能力可能是一个安全问题。此外,即使这没有问题,在全局范围内进行这种替换仍然没有用。它会影响您的整个领域,包括您正在使用的所有库。他们可能依赖于使用原生 promise 。而且您不能使用两个不同的 Promise 库,尽管它们可能是必需的。
I want to be able to do something like:
getResolvedAsyncAwaitPromise().tap(...)
你可以做的就是用Promise.method
将函数包装在它的定义中。 :
const Bluebird = require('Bluebird');
const getResolvedAsyncAwaitPromise = Bluebird.method(async () => 'value');
getResolvedAsyncAwaitPromise()
.tap(…) // Works!
.then(…);
关于node.js - 从异步等待函数中获取 Bluebird Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44158629/
bluebird.js和bluebird.core.js有什么区别? 什么时候应该使用bluebird.core.js而不是bluebird.js? 我无法在bluebird site或其他地方找到任
我正在从异步转换为 Bluebird,但不知道如何打破循环 这是我想要实现的目标: 循环数据数组。 对于每个项目,检查它是否存在于数据库中。 将一个项添加到数据库(第一个不存在的项),然后退出.eac
我试图用 .try(function(){}).catch(function(){}) 块返回一个 promise 。我的问题是由我的 promise 类型引起的。 deleteProcess
我正在尝试使用 bluebird 的 .return() 来扩展 promise 解析值方法。 目前我正在使用以下代码: doSomethingAsync() // assu
我刚刚开始使用 Promise 和 Bluebird。调试时我可以看到我的函数执行了两次: 首先我收到此错误:TypeError:未捕获错误:无法读取未定义的属性“then” 然后我看到函数再次执行,
我想测试数组的每个元素,直到满足条件,然后跳过其余的。这是我想出的代码,它似乎有效,但我不确定它是否真的安全或有意想不到的副作用。欢迎其他解决方案。 let buddyAdded = false; r
假设我有以下 node.js 代码 function foo() { return promiseOne().then(function(result) { return pr
假设我想在从数据库查找用户后同时发送电子邮件并向客户端推送通知,我可以这样写 User.findById(userId).exec() .then(() => sendMail()) .then(()
.call 方法的 Bluebird 文档有 code sample标记为“链接破折号或下划线方法”。 下面的代码片段中链接的 .then(_) 的用途是什么? var Promise = requi
是否有某种方法可以检索从上一个 then 回调返回的任何内容(或传递给初始 Promise.resolve()/resolve())? const p = Bluebird.resolve().the
我有以下代码。当 f2 没有抛出错误时,它工作正常。 如果有错误,它会生成一个Unhandled rejection Error。 重写代码以避免 Unhandled rejection Error
我有一个来自这篇文章的后续问题:Chaining Requests using BlueBird/ Request-Promise 我对 promise 很陌生,所以请原谅我的天真。我成功地实现了这段
我是 Bluebird 的新手,我正在尝试创建一个新用户,但 reject 函数没有按预期工作。 问题是它为我创建了用户,即使它启动了错误There nickname is already in us
我正在尝试实现剪刀石头布游戏的 CLI 版本。我正在使用查询器模块来处理 IO。我的主要功能如下所示: RockPaperScissors.prototype.gameLoop = function(
此代码运行正常: let promise; try { promise = parent(); //but I want: await parent(); await cont
在 promise 了 fs-extra 后,我知道我可以使用 then 来访问文件。我猜想有某种机制,在获取文件后,它知道要移动到 then 链中的下一个链接。然而,接下来的then我只是放置了一个
我有以下代码。它按预期工作,没有抛出未处理的拒绝错误。 p = new Promise (fulfill, reject) -> reject new Error 'some error' p.c
我期待 Bluebird forgotten return warning出现,但由于某种原因它不起作用。 A demo : const Bluebird = require('bluebird');
我正在使用 bluebird图书馆结束memcached . memcached.set('foo', 'bar', 10, function (err) { /* stuff */ }); 此函数不
我正在尝试如下使用 Bluebird 的协程: var p = require('bluebird'); //this should return a promise resolved to valu
我是一名优秀的程序员,十分优秀!