Last active
August 29, 2015 14:12
-
-
Save yicone/5ad5b2ac77db0ccfa225 to your computer and use it in GitHub Desktop.
async waterfall sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async.waterfall([ | |
function (cb) { | |
app.service.pay(this.data, { | |
headers: { | |
"Session-Id": app.sessionId | |
}, | |
success: function (data, textStatus, jqXHR) { | |
console.log('pay resp textStatus:' + textStatus); | |
console.log('pay resp data', data); | |
cb(null, data); | |
}, | |
error: function (jqXHR, textStatus, errorThrown) { | |
console.error('pay', JSON.stringify(jqXHR)); | |
var err = errorThrown || 'pay resp unknown error'; | |
cb(err); | |
} | |
}); | |
}, | |
function (data, cb) { | |
if (payMethod != 2) return cb(null, data); | |
// !暂不考虑包含短代的组合支付 | |
var smsPaymentInfo = data.return.payments[0]; | |
var extResponse = JSON.parse(smsPaymentInfo.extResponse); | |
console.log('extResponse', extResponse); | |
var smsTask = { | |
phoneNumber: extResponse.channel, | |
message: extResponse.sms, | |
pendingInterceptTasks: extResponse.removeSms, | |
nextTaskTicket: JSON.stringify({ | |
id: extResponse.interactId | |
}) | |
}; | |
async.waterfall([function (cb) { | |
plugins.sms.sendMessage(smsTask, function (result) { | |
console.log('sendMessage', result); | |
if (!result.nextTaskTicket || !result.nextTaskTicket.id) { | |
cb(); | |
} | |
}); | |
}, function (data, cb) { | |
app.service.interactAfterPay(result, { | |
headers: { | |
"Session-Id": app.sessionId | |
}, | |
success: function (data, textStatus, jqXHR) { | |
console.log('interactAfterPay', data); | |
cb(null, data); | |
}, | |
error: function (jqXHR, textStatus, errorThrown) { | |
console.error('interactAfterPay', JSON.stringify(jqXHR)); | |
var err = errorThrown || 'interactAfterPay resp unknown error'; | |
cb(err); | |
} | |
}); | |
}, function (data, cb) { | |
smsTask = { | |
phoneNumber: data.channel, | |
message: data.sms, | |
pendingInterceptTasks: data.removeSms | |
}; | |
plugins.sms.sendMessage(smsTask, function (result) { | |
console.log('sendMessage', result); | |
cb(); | |
}, function (result) { | |
console.error('sendMessage', result); | |
cb(result); | |
}); | |
}], function (err, data) { | |
cb(err, data); | |
}); | |
}], function (err, data) { | |
if (err) { | |
console.error('pay', err); | |
showResult('支付失败:' + err); | |
busyIndicator.hideBusy(); | |
return; | |
} | |
showResult('支付成功'); | |
busyIndicator.hideBusy(); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment