Promise 保持待处理状态,返回 Null
我正在研究GraphQl查询,我正在尝试找到一个唯一的模型。但是,由于查询完成之前的代码一直在继续,因此什么都没有返回,因此在预期模型时试图回报承诺。代码看起来如下...
const findShift = async (date) => {
console.log("In mutation function")
const foundShift = await db.shift.findUnique({
where: {
date: date
}
})
return foundShift
}
const foundShift = findShift(date).then( resolved => {
console.log("printing resolved...")
console.log(resolved)
if (resolved.id != 'undefined'){
console.log({
id: resolved.id,
date: resolved.date,
allDevices: resolved.allDevices
})
return foundShift
}
else{
throw new Error("no shift of that date found!")
}
})
和console.log语句使控制台看起来像这样...
In mutation function
Promise { <pending> }
prisma:info Starting a postgresql pool with 9 connections.
最终查询只是返回null
。如您所见,我尝试使用然后尝试使用
,并将突变本身放入完全不同的功能中,只是为了避免这些异步问题而无济于事。有人看到解决方法吗?
I am working on a GraphQL query where I am trying to find a unique model. However, nothing ever gets returned because the code kept carrying on before the query was finished, thus attempted to return a Promise when it expected a Model. The code looks as follows...
const findShift = async (date) => {
console.log("In mutation function")
const foundShift = await db.shift.findUnique({
where: {
date: date
}
})
return foundShift
}
const foundShift = findShift(date).then( resolved => {
console.log("printing resolved...")
console.log(resolved)
if (resolved.id != 'undefined'){
console.log({
id: resolved.id,
date: resolved.date,
allDevices: resolved.allDevices
})
return foundShift
}
else{
throw new Error("no shift of that date found!")
}
})
And the console.log statements make the console look as so...
In mutation function
Promise { <pending> }
prisma:info Starting a postgresql pool with 9 connections.
and ultimately the query just returns null
. As you see, I tried using then
and putting the mutation itself into an entirely different function just to circumvent these asynchronisity issues to no avail. Does anyone see a workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,所有
async
函数返回承诺。异步函数中的返回值成为该诺言的解决值。因此,async
函数的呼叫者必须使用。然后,然后等待从异步函数获取已解析值。像您正在尝试的那样,无法“绕开”异步性。您可以驯服它以使其更有用,但是您无法逃脱它。因此,您的
async
函数返回待处理的承诺,最终将解决您在async
函数中返回的任何值。您可以阅读有关
async
函数工作的更多信息在这里在其他答案中。试图使a 最小,可重复的示例用异步模拟替换为数据库调用:
当我在nodejs中运行此错误时,我会收到此错误:
并且,错误是由这条代码线引起的:
您正在尝试返回该承诺链的一部分,该诺言已经是该承诺链的一部分。承诺链。这会产生不允许的循环依赖性。
您需要返回的是您想要父母承诺的解决价值的一切。由于这看起来像是您在其上方构造的对象,因此我修改了代码来执行此操作。可以运行此代码,
fistshift
是解决您对象的承诺。以下是有关承诺可能有帮助的承诺的几个规则:
fn()。然后()
或fn()。catch()返回的
fn()
。async
函数返回承诺。等待
只能在async
函数(或在ESM模块的顶级)中使用。等待
暂停执行async
函数,然后立即向呼叫者返回未实现的承诺。因此,等待
仅影响当前功能流,而不影响呼叫者的流程。呼叫者仍然必须使用。然后,然后使用()
或等待
从async
函数返回的承诺中获取值。First off, ALL
async
functions return a promise. The return value in the async function becomes the resolved value of that promise. So, the caller of anasync
function MUST use.then()
or await to get the resolved value from the async function. There is no way to "circumvent" the asynchronicity like you are attempting. You can tame it to make it more usable, but you can't escape it. So, yourasync
function returns a pending promise that will eventually resolve to whatever value you return inside yourasync
function.You can read more about how
async
functions work here in this other answer.In trying to make a minimal, reproducible example of your code, I've reduced it to this where I've substituted an asynchronous simulation for the database call:
When I run this in nodejs, I get this error:
And, the error is caused by this line of code:
You are attempting to return a promise that's already part of this promise chain from within the promise chain. That creates a circular dependency which is not allowed.
What you need to return there is whatever you want the resolved value of the parent promise to be. Since that looks like it's the object you construct right above it, I've modified the code to do that. This code can be run and
foundShift
is a promise that resolves to your object.Here are a couple of rule about promises that might help:
fn().then()
orfn().catch()
calls return a new promise that is chained to the one thatfn()
returned.async
functions return a promise.await
can only be used inside anasync
function (or at the top level of an ESM module).await
in a function suspends execution of theasync
function and then immediately returns an unfulfilled promise to the caller. So, theawait
only affects the current function flow, not the caller's flow. The caller will still have to use.then()
orawait
to get the value out of the promise that theasync
function returns.