无法修改javascript回调中的值[重复]

时间:2022-03-06 17:04:21

This question already has an answer here:

这个问题在这里已有答案:

I am not able to modify the value of outputString variable inside callback.

我无法在回调中修改outputString变量的值。

var outputString;

    client.get(key,function(err,value){

                        outputString = "key="+key+" value="+value ;
                        console.log(outputString);

                    })

console.log(outputString);

When I print the value of outputString, it says "undefined"

当我打印outputString的值时,它表示“未定义”

1 个解决方案

#1


0  

client.get() returns immediately, so outputString hasn't been set when the last console.log() is called. The callback function isn't called until sometime later.

client.get()立即返回,因此在调用最后一个console.log()时尚未设置outputString。直到某个时候才会调用回调函数。

#1


0  

client.get() returns immediately, so outputString hasn't been set when the last console.log() is called. The callback function isn't called until sometime later.

client.get()立即返回,因此在调用最后一个console.log()时尚未设置outputString。直到某个时候才会调用回调函数。