2017-08-16 113 views
-4

的getAsync功能等则已经被执行后,完成它的执行。一个人应该先完成然后再去另一个?怎么回事?我怎样才能解决这个问题?为什么我的承诺不工作?

function checkRedis() { 
 
\t \t return new Promise(resolve => { 
 
\t \t \t \t \t 
 
\t \t \t redis.set('kleberIsTheBest', 'heSureIs') 
 
\t \t \t \t .then(() => { 
 
\t \t \t \t \t console.log('1') 
 
\t \t \t \t \t redis.getAsync('bananas').then(data => { 
 

 
\t \t \t \t \t \t if(!data){ 
 
\t \t \t \t \t \t \t console.log('I got here bb') 
 
\t \t \t \t \t \t \t resolve('DOWN') 
 
\t \t \t \t \t \t } 
 

 
\t \t \t \t \t \t console.log(`here is the ${data}`) 
 
\t \t \t \t \t }) 
 
\t \t \t \t }) \t 
 
\t \t \t \t .then((x) => { 
 
\t \t \t \t \t console.log('2 ' + x) 
 
\t \t \t \t \t redis.del('kleberIsTheBest') 
 
\t \t \t \t \t console.log('i got here too bb') 
 
\t \t \t \t \t resolve('UP') 
 
\t \t \t \t }) 
 
\t \t \t \t .catch(() => { 
 
\t \t \t \t \t console.log('uh oh... something went bad!') 
 
\t \t \t \t \t resolve('DOWN') 
 
\t \t \t \t }) 
 
\t \t \t }) 
 
\t }

在一个侧面说明,莫非是抓工作的?无法模拟错误落入捕获处理程序。

+0

是应该如何等待的东西,它没有一个参考?你为什么多次调用'resolve'? –

+0

我打电话多次解析,因为它应该根据服务器状态返回UP或DOWN。 –

+0

我不认为你需要调用解决方案。我不认为你需要新的承诺。 redis.set已经创建了一个承诺。 –

回答

-1

目前尚不清楚你正在使用的客户端Redis的;但是,如果你正在使用node_redis(这是recommended client为节点),然后set()不返回的承诺。该set()功能需要一个回调

如果您使用bluebird,则可以使用promisesnode_redis。 Promisifying node_redis使用蓝鸟将增加功能与异步后缀;所以对于set()函数,将会有一个名为setAsync()

这里有两种方法可以使用node_redis

随着回调

var redis = require('redis'); 
var client = redis.createClient(); 

function checkRedis(callback) { 
    client.set('kleberIsTheBest', 'heSureIs', callback); 
}; 

checkRedis(function(error, result) { 
    if (error) { 
     console.error(error); 
    } else { 
     console.log('Done'); 
     console.log(result); 
    } 
}); 

随着无极

var redis = require('redis'); 
var bluebird = require('bluebird'); 

bluebird.promisifyAll(redis.RedisClient.prototype); 
bluebird.promisifyAll(redis.Multi.prototype); 

var client = redis.createClient(); 

function checkRedis() { 
    return client.setAsync('kleberIsTheBest', 'heSureIs'); 
} 

checkRedis() 
.then(function(result) { 
    console.log('Done'); 
    console.log(result); 
}) 
.catch(function(error) { 
    console.error(error); 
}); 
+0

这不是OP的问题。他没有得到某种“不能调用方法”,然后......“类型错误,他抱怨说它没有按照预期的顺序运行。 – Bergi

+0

@Bergi那很好,我知道;然而,我向OP展示了一个使用其中一个客户端的例子,因为他没有说明他使用的是哪个Redis客户端。那么,我的回答是否被拒绝? – mscheker

+0

如果您想要了解有关使用的客户端的说明,请使用注释。如果你想给无关的东西提供建议,请使用注释(可能带有代码链接)。是的,我做了downvote,因为我认为这个答案不能为这个问题提供有用的解决方案。 (我不是说这是错误的或任何东西) – Bergi

0

好了,伙计们。我得到了答案。

大家很奇怪,都是我所要做的就是返回Redis的getAsync承诺,现在它的工作就像一个魅力!

function checkRedis() { 
 
\t \t return new Promise(resolve => { 
 
\t \t \t \t \t 
 
\t \t \t redis.set('kleberIsTheBest', 'heSureIs') 
 
\t \t \t \t .then(() => { 
 
\t \t \t \t \t return redis.getAsync('kleberIsTheBest') \t \t \t \t \t 
 
\t \t \t \t }) 
 
\t \t \t \t .then(data => { 
 

 
\t \t \t \t \t console.log(`here is the ${data}`) 
 

 
\t \t \t \t \t if(!data){ 
 
\t \t \t \t \t \t resolve('DOWN') 
 
\t \t \t \t \t } 
 

 
\t \t \t \t }) 
 
\t \t \t \t .then((x) => { 
 
\t \t \t \t \t redis.del('kleberIsTheBest') 
 
\t \t \t \t \t resolve('UP') 
 
\t \t \t \t }) 
 
\t \t \t \t .catch(() => { 
 
\t \t \t \t \t console.log('uh oh... something went bad!') 
 
\t \t \t \t \t resolve('DOWN') 
 
\t \t \t \t }) 
 
\t \t \t }) 
 
\t }