到目前为止,我一直使用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/