Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

javascript - 承诺未解决,但在promise回调内部达到了resolve()(Promise not resolving but resolve() is being reached inside of the promise callback)

I'm trying to write a query builder similar to the query builder in Laravel but for TypeScript but I have come across a problem with a promise resolving.(我正在尝试编写与Laravel中的查询生成器类似的查询生成器,但是对于TypeScript,但是遇到了Promise解决的问题。)

In this code below I have cut down my builder and controller classes.(在下面的代码中,我减少了构建器和控制器类。) Using this library https://github.com/mysqljs/mysql , I run a query using the code connection.query(...) which I pass in a callback function which processes the response and resolves/rejects accordingly.(使用此库https://github.com/mysqljs/mysql ,我使用代码connection.query(...)运行查询,该查询传递给回调函数,该函数处理响应并相应地解析/拒绝。) The thing I don't understand is that it reaches the console.log and outputs it into my console but the .then() in the controller is not being called.(我不明白的是,它到达console.log并将其输出到我的控制台中,但是未调用控制器中的.then() 。) import * as mysql from 'mysql'; interface InternalQueryReturnType<T> { results: T; fields?: mysql.FieldInfo[]; } // Cut down version of my builder. export class MySQLBuilder implements IQueryBuilder { protected query<T>(query: string): Promise<InternalQueryReturnType<T>> { return new Promise((resolve, reject) => { const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'dbname', port: 999, }); connection.connect({}, (error) => { reject(error); }); connection.query(query, (error, results, fields) => { if (error) { reject(error); return; } // It is reaching this point. console.log({ results }); // <- This is returning the correct results. resolve({ results, fields }); }); connection.end(); }); } } // My controller. export class PostController { public static index(request: IRequest): Promise<IResponse> { return new Promise((resolve, reject) => { return new Promise((resolve) => { // Testing the raw query which the builder created. new MySQLBuilder().query<any>('SELECT * FROM `users` WHERE `id` = 1 LIMIT 1 OFFSET 0') .then(({ results }) => { // Nothing was passed into this. console.log({ results }); resolve(new JsonResponse(results || {})); }) .catch((error) => { resolve(new JsonResponse(error).status(400)); }); // Builder code. // new MySQLBuilder().table('users') // .where('id', '=', 1) // .first() // .then((value: object | null) => { // console.log(value); // resolve(new JsonResponse(value || {})); // }) // .catch((error) => { // console.error({ error }); // resolve(new JsonResponse(error).status(400)); // }); }); }); } } Has anyone experienced this issues with promises?(有没有人遇到过诺言的问题?) If you need the entire code base I can put it all up later but this should be enough code.(如果您需要整个代码库,我可以稍后再进行整理,但这应该足够了。) The solution to this issue was actually this:(这个问题的解决方案实际上是这样的:) connection.connect({}, (error) => { if (error) { reject(error); } });   ask by Isaac Skelton translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The solution to this issue was actually this:(这个问题的解决方案实际上是这样的:)

connection.connect({}, (error) => { if (error) { reject(error); } }); I wasn't checking if error was NULL.(我没有检查错误是否为NULL。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...