javascript - 使用没有 Promise 的解决方案

标签 javascript graphql

到目前为止,我一直使用resolve和promise。

最初,当我研究 Promise 时,我读到一旦我们获得了值,我们就需要解决 Promise。

现在,我正在研究 GraphQL,其中讲师在没有 promise 的情况下使用解析,即类似于

const RootQuery = new GraphQLObjectType({
    name: "RootQueryType",
    fields: {
        book: {
         args: { id: { type: GraphQLString }},
         resolve(parent, args){ 
            for (let i = 0; i<books.length; i++) {
                    if ( books[i]["id"] == args ) {
                        return books[i]["id"]
                    }
                }
            }
        }
    }    

})

所以我的问题是,我们通常什么时候使用resolve或者为什么在这里使用resolve。

我正在看这个youtube video大约52分钟

最佳答案

When I was studying about Promises, I read that we need to resolve the promise once we have the value.

是的。

Now, I was studying about GraphQL where the instructor using resolve without a promise

没有。

他们正在定义一个名为resolve的函数。

resolve 不是保留关键字。这只是一个名字。

它与用于解决 promise 的函数完全无关。

const example = {
    resolve(a, b) {
       return a + b;
    }
};

console.log(example.resolve(1,2));

关于javascript - 使用没有 Promise 的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52814131/

相关文章:

javascript - 如何从下拉列表中删除选定值以外的所有值

symfony - 如何启用普通 id 查询而不是在 api_platform graphql 中使用路径?

django - Graphite 烯错误消息

javascript - Firebase 是否发送给定路径下的所有数据?

javascript - 转换 SCORM.js 以与 Unity 5 配合使用

javascript - 单击折线时如何显示信息窗口

json - 带有桑格利亚汽酒的 GraphQL 模式

graphql 必填字段方法

angular - 当我从 Angular 传递 graphql 查询时,为什么 GraphQL Controller 中的参数查询为空?

javascript - 如何在保留选定单元格的同时重新加载 Google Charts 日历中的数据?