gpt4 book ai didi

javascript - redis hmget node.js 异步/等待不工作

转载 作者:可可西里 更新时间:2023-11-01 11:23:25 26 4
gpt4 key购买 nike

我正在尝试使用以下代码通过 node.js 从 redis 获取一些数据:

var moment = require("moment");
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var crypto = require('crypto');
var redis = require('redis');
var bluebird = require("bluebird");
bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);


const uuidv4 = require('uuid/v4');

var KEY_USERS = "auction:users";
var KEY_ADMIN_TOKENS = KEY_USERS + ":admin_tokens";
var KEY_USER_TOKENS = KEY_USERS + ":user_tokens";
var SERVER_PORT = 12251;

var redis_client = bluebird.promisifyAll(redis.createClient({
host: '127.0.0.1',
no_ready_check: true,
auth_pass: '****',
}));



redis_client.on('connect', function () {
console.log('Redis client connected');
});

redis_client.on('error', function (err) {
console.log('Something went wrong with Redis Connection ' + err);
});

server.listen(SERVER_PORT);

console.log('1');

(async () => {

var response = await isAdminUser('c169024a-8902-4310-8955-ff46516e8703');

console.log("From Async " + response);
})()

console.log('2');

// Promise.resolve()
// .then(() => isAdminUser('c169024a-8902-4310-8955-ff46516e8703'))
// .then(() => console.log('3'));


console.log("Server Connected and redis initialized.");

async function isAdminUser(token) {
return new Promise((resolve, reject) => {

redis_client.hmget(KEY_ADMIN_TOKENS, [token, token], function (error, result) {
if (error) {
console.log(error);
throw error;
}
console.log("Token Result : " + result);

resolve(result);
});
});
}

现在的问题是,node 正在等待 redis 查询完成并前进到下一行代码。

控制台输出:

1
2
Server Connected and redis initialized.
Redis client connected
Token Result : "my result"
From Async "my result"

期望的输出:

1
2
Redis client connected
Token Result : "my result"
From Async "my result"
Server Connected and redis initialized.

我错过了什么/做错了什么?我已经阅读了很多建议相同的教程/文章。

最佳答案

异步函数只不过是返回 promise 的函数的语法糖,只有在函数中使用 await 时才使函数异步才有意义。

您的函数 isAdminUser 不使用 await,因此它不需要 async。它实际上并没有在那里做任何事情。

此外,您正在将您的 redis_client 传递给 promisifyAll,但要使其生效,您需要使用 async 中的方法客户。

我稍微重构了你的代码,将客户端初始化转移到一个 promise 中:

const initializeRedisClient = () => new Promise((resolve, reject) => {
const redis_client = bluebird.promisifyAll(redis.createClient({
host: '127.0.0.1',
no_ready_check: true,
auth_pass: '****',
}));

redis_client.on('connect', function () {
console.log('Redis client connected');
resolve(redis_client);
});

redis_client.on('error', function (err) {
console.log('Something went wrong with Redis Connection ' + err);
reject(err);
});
});

(async () => {
const redis_client = await initializeRedisClient();
const response = await isAdminUser(redis_client, 'c169024a-8902-4310-8955-ff46516e8703');

console.log("From Async " + response);
console.log("Server Connected and redis initialized.");
})()

console.log('2');

async function isAdminUser(redis_client, token) {
const result = redis_client.hmgetAsync(KEY_ADMIN_TOKENS, [token, token]);

console.log("Token Result : " + result);

return result;
}

关于javascript - redis hmget node.js 异步/等待不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56717228/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com