gpt4 book ai didi

javascript - meteor 回调 天上回调又回调

转载 作者:行者123 更新时间:2023-12-03 10:45:57 25 4
gpt4 key购买 nike

我设置了 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);

最佳答案

Meteor 为回调 hell 提供了很好的解决方案。

如果您已经使用 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);

关于javascript - meteor 回调 天上回调又回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28571434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com