meteor callback heaven calling a callback after another callback

有些话、适合烂在心里 提交于 2019-12-23 04:33:08

问题


I have set up 3 functions where the need to run after each other

I have set up my callback correctly and able to invoke functionTwo after functionOne like this...

functionOne(functionTwo) // this is fine

How do invoke functionThree after functionTwo?

I tried functionOne(functionTwo(functionThree)) but this is wrong

My code

   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);

回答1:


Meteor gives great solution for callback hell.

If you already wrapped functions with Meteor.wrapAsync, then you should be able to run them like this (pseudo code):

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);
} 

If you want to run them as above, you need implement your functions in the way like this:

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);


来源:https://stackoverflow.com/questions/28571434/meteor-callback-heaven-calling-a-callback-after-another-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!