2015-02-17 90 views
0

我已成立了3個功能,其中需要相互流星迴調天堂調用回調另一個回調後

運行後我正確設置了我的回調,並能夠像這樣functionOne之後調用functionTwo ...

functionOne(functionTwo) // this is fine 

如何在functionTwo後調用functionThree?

我試圖functionOne(functionTwo(functionThree))但這是錯誤的

我的代碼

var functionOneAsync = function(callback) { 
    request.post('http://www.goodreads.com/oauth/access_token', { 
    oauth: {consumer_key: 'somekey', 
      consumer_secret: 'somesecretkey', 
      token: oauthToken, 
      token_secret: oauthTokenSecret} 
    }, 
    function (error, response, body){ 
    if (!error && response.statusCode === 200) { 
     var perm_data = querystring.parse(body) 
     var accessToken = perm_data.oauth_token 
     var tokenSecret = perm_data.oauth_token_secret 
     console.log('exch done') 
     return callback(null, accessToken, tokenSecret) 
    } 
    else { 
     callback(error) 
    } 
    }) 
} 
var functionOneAsyncSync = Meteor.wrapAsync(functionOneAsync); 

var functionTwoAsync = function(error, accessToken, tokenSecret, callback) { 
    request.get('https://www.goodreads.com/api/auth_user', { 
    oauth: {consumer_key:'somekey', 
      consumer_secret:'somesecretkey', 
      token: accessToken, 
      token_secret: tokenSecret 
      } 
    }, 
    function (error, response, body) { 
    if (!error && response.statusCode === 200) { 
     var options = { 
     object: true, 
     reversible: false, 
     coerce: true, 
     sanitize: true, 
     trim: true, 
     arrayNotation: false 
     } 
     var bodyToJson = parser.toJson(body, options) 
     var goodreadsUserId = bodyToJson.GoodreadsResponse.user.id 
     console.log('user id done' + goodreadsUserId) 
     return callback(null, accessToken, tokenSecret, goodreadsUserId) 
     } 
    else { 
     return callback(error) 
    } 
    }) 
} 
var functionTwo = Meteor.wrapAsync(functionTwoAsync); 



var functionThreeAsync = function(error, accessToken, tokenSecret, goodreadsUserId) { 
    if (error) { 
    console.log('error in storing') 
    console.log(error) 
    } 
    else { 
    Meteor.users.update({_id: Meteor.userId()}, {$set: {'profile.goodreads.accessToken': accessToken, 'profile.goodreads.tokenSecret': tokenSecret, 'profile.goodreads.userID': goodreadsUserId}}); 
    console.log('reached storing in user collection') 
    } 
} 
var functionThree = Meteor.wrapAsync(functionThreeAsync); 
+0

顯示@Yuri我已經貼了functionTwo – 2015-02-17 21:34:49

+0

代碼,請參見上面 – meteorBuzz 2015-02-17 21:40:33

回答

1

流星給出了回調地獄很好的解決方案。

如果你已經包裹功能與Meteor.wrapAsync,那麼你應該能夠這樣運行了他們(僞代碼):

try { 
var resultOne = functionOne(); 
var resultTwo = functionTwo(resultOne.accessToken, resultOne.tokenSecret, ...) 
var resultThree = functionThree(resultOne.accessToken, resultOne.tokenSecret, ...) 
} catch (e) { 
    console.error('functionOne or functionTwo or functionThree thrown error = ', e); 
} 

如果你想運行它們如上,你需要實現你的功能在這樣的方式:

var functionTwoAsync = function(accessToken, tokenSecret, callback) { 
    request.get('https://www.goodreads.com/api/auth_user', { 
    oauth: {...} 
    }, 
    function (error, response, body) { 
    if (!error && response.statusCode === 200) { 

     // IMPORTANT !!! 
     return callback(
      null, 
      { 
       accessToken : accessToken, 
       tokenSecret : tokenSecret, 
       goodreadsUserId : goodreadsUserId 
      } 
    ) 
    } else { 
     return callback(error) 
    } 
    }) 
} 
var functionTwo = Meteor.wrapAsync(functionTwoAsync); 
+0

是否有必要通過數值都在同一個對象到下一個功能?或者我可以保持簡單,因爲我已經完成了多個值的傳遞?我會嘗試你的方法,並讓你知道我如何繼續。感謝您的輸入 – meteorBuzz 2015-02-17 23:40:09

+0

如果您想從前一個函數返回多個變量,那麼這是必要的。 – 2015-02-18 06:49:09

+0

這是否意味着我的函數三隻是一個錯誤和結果參數。包含三個字段accessToken,tokenSecret和goodreadsUserId的對象的結果參數? – meteorBuzz 2015-02-18 10:20:14