我必须将一个函数传递给另一个函数,并将其作为回调执行.问题是有时这个函数是异步的,比如:
async function() {
// Some async actions
}
Run Code Online (Sandbox Code Playgroud)
所以我想执行await callback()或callback()取决于它接收的函数类型.
有没有办法知道函数的类型?
我正在尝试Relay和GraphQL.当我在做模式时,我这样做:
let articleQLO = new GraphQLObjectType({
name: 'Article',
description: 'An article',
fields: () => ({
_id: globalIdField('Article'),
title: {
type: GraphQLString,
description: 'The title of the article',
resolve: (article) => article.getTitle(),
},
author: {
type: userConnection,
description: 'The author of the article',
resolve: (article) => article.getAuthor(),
},
}),
interfaces: [nodeInterface],
})
Run Code Online (Sandbox Code Playgroud)
所以,当我要求这样的文章时:
{
article(id: 1) {
id,
title,
author
}
}
Run Code Online (Sandbox Code Playgroud)
它会对数据库进行3次查询吗?我的意思是,每个字段都有一个解析方法(getTitle,getAuthor ...),它向数据库发出请求.我做错了吗?
这是getAuthor的一个例子(我使用mongoose):
articleSchema.methods.getAuthor = function(id){
let article = this.model('Article').findOne({_id: id})
return article.author
}
Run Code Online (Sandbox Code Playgroud)