meteor回调天堂在另一个回调之后调用一个回调 [英] meteor callback heaven calling a callback after another callback

查看:112
本文介绍了meteor回调天堂在另一个回调之后调用一个回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了3个函数,其中需要彼此之间运行

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

我已经正确设置了回调,并能够调用functionTwo后functionOne喜欢这个.. 。

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

functionOne(functionTwo) // this is fine

如何在functionTwo之后调用functionThree?

How do invoke functionThree after functionTwo?

我试过 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);


推荐答案

如果您已经使用Meteor.wrapAsync包装函数,那么您应该能够像下面这样运行它们(伪代码):

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

这篇关于meteor回调天堂在另一个回调之后调用一个回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆