2017-11-21 2 views
0

redis npm 모듈을 사용 중입니다. 콜백에 일부 데이터 (예 : for 루프 색인)를 전달하여 로컬 데이터를 매핑 할 수 있습니다. 함수 호출)를 응답 데이터로 변환합니다. 예 :노드 redis 콜백에 데이터 전달

redisClient.get('a', [data1, data2], function(err, res, data1, data2){ 

/*more code */ 
}) 

나는 문서를 살펴 봤지만 그 내용을 찾을 수 없습니다.

+0

콜백 내에서 배열을 정의하고 액세스 할 수없는 이유는 무엇입니까? – kgangadhar

+0

내가 할 수있는 일이지만, 배열의 어떤 항목이 현재 콜백 실행에 액세스해야하는지 어떻게 알 수 있습니까? – Andy

답변

0

약속이 깨끗하기 때문에 대신 사용하는 것이 좋습니다.

다음은 그냥 단순히 기능의 요청을 포장 결국

const Promise = require('bluebird') 
const redis = require('redis'); 
Promise.promisifyAll(redis.RedisClient.prototype); 
Promise.promisifyAll(redis.Multi.prototype); 

const client = redis.createClient() // U can put your redis credentials here. 

async function someFunc() { 
    try { 
    for (const i = 0; i < 10; i++) { 
     const result = await client.getAsync() 
     // do stuffs with your result 
    } 
    } 
    catch (error) { 
    // handle your error 
    } 
} 
0
function redisGet(_data1, _data2, callback){ 
    redisClient.get('a', function(err, res){ 
    callback(err, res, _data1, _data2) 
    }) 
} 

redisGet(data1, data2, function(err, res, data1, data2){ 

    /*more code*/ 
}) 

짧은 예이다.