Javascript 如何使用需要两个异步方法的node/express处理请求
我在学习节点。我有一个web应用程序,它通过包与Bitcoin和PostgreSQL接口。我需要从每个模块中获取一点数据,然后将其全部传递到视图进行渲染。到目前为止,我的代码如下所示:Javascript 如何使用需要两个异步方法的node/express处理请求,javascript,node.js,asynchronous,promise,Javascript,Node.js,Asynchronous,Promise,我在学习节点。我有一个web应用程序,它通过包与Bitcoin和PostgreSQL接口。我需要从每个模块中获取一点数据,然后将其全部传递到视图进行渲染。到目前为止,我的代码如下所示: exports.index = function(req, res){ var testnet=''; bitcoin.getInfo(function(e, info){ if(e){ return console.log(e);} if(info.testnet){ tes
exports.index = function(req, res){
var testnet='';
bitcoin.getInfo(function(e, info){
if(e){ return console.log(e);}
if(info.testnet){
testnet='bitcoin RPC is testnet';
}else{
testnet='nope.. you must be crazy';
}
var c=knex('config').select().then(function(k){
res.render('index', { title: k[0].site_name, testnet: testnet });
});
});
};
但按照这种结构,它将首先等待比特币回复,然后向PostgreSQL发出请求,然后再等待一段时间,等待比特币回复。显然,这两个等待期可能同时发生。但是,我不知道如何使用Javascript中的承诺/回调实现这一点。我怎样才能异步地而不是串行地处理这种情况呢?您可以使用类似的库
#parallel()
方法,也可以浏览它的源代码,学习如何扮演自己的角色您可以使用类似的库#parallel()
方法,或者您可以浏览其源代码,学习扮演自己的角色您正在寻找一个模块,该模块将让您完成两项任务,然后继续
这是一个未经测试的示例,可以为您提供以下想法:
exports.index = function(req, res){
var testnet='', k={};
async.parallel([
function(){
bitcoin.getInfo(function(e, info){
//your getInfo callback logic
},
function(){
knex('config').select().then(function(result) {
//your knex callback
k = result;
} ],
//Here's the final callback when both are complete
function() {
res.render('index', { title: k[0].site_name, testnet: testnet });
});
}
您正在寻找一个模块,该模块将允许您启动两个任务,然后继续
这是一个未经测试的示例,可以为您提供以下想法:
exports.index = function(req, res){
var testnet='', k={};
async.parallel([
function(){
bitcoin.getInfo(function(e, info){
//your getInfo callback logic
},
function(){
knex('config').select().then(function(result) {
//your knex callback
k = result;
} ],
//Here's the final callback when both are complete
function() {
res.render('index', { title: k[0].site_name, testnet: testnet });
});
}
您需要承诺回调方法,而不是混合回调和承诺
var Promise = require("bluebird");
var bitcoin = require("bitcoin");
//Now the bitcoin client has promise methods with *Async postfix
Promise.promisifyAll(bitcoin.Client.prototype);
var client = new bitcoin.Client({...});
那么实际代码是:
exports.index = function(req, res) {
var info = client.getInfoAsync();
var config = knex('config').select();
Promise.all([info, config]).spread(function(info, config) {
res.render('index', {
title: config[0].site_name,
testnet: info.testnet ? 'bitcoin RPC is testnet' : 'nope.. you must be crazy'
});
}).catch(function(e){
//This will catch **both** info and config errors, which your code didn't
//do
console.log(e);
});
};
使用Promise模块。您需要推荐回调方法,而不是混合回调和承诺
var Promise = require("bluebird");
var bitcoin = require("bitcoin");
//Now the bitcoin client has promise methods with *Async postfix
Promise.promisifyAll(bitcoin.Client.prototype);
var client = new bitcoin.Client({...});
那么实际代码是:
exports.index = function(req, res) {
var info = client.getInfoAsync();
var config = knex('config').select();
Promise.all([info, config]).spread(function(info, config) {
res.render('index', {
title: config[0].site_name,
testnet: info.testnet ? 'bitcoin RPC is testnet' : 'nope.. you must be crazy'
});
}).catch(function(e){
//This will catch **both** info and config errors, which your code didn't
//do
console.log(e);
});
};
使用承诺模块。您对Postgre的请求是否依赖比特币的响应?@r3mus并不总是如此。有时它可以是平行的,如果不是,那么乔的答案肯定是正确的。如果它确实回复了响应,那么您将无法运行这些异步。您对Postgre的请求是否依赖于比特币的响应?@r3mus并不总是如此。有时它可以是平行的,如果不是,那么乔的答案肯定是正确的。如果它确实对响应进行了回复,那么您将无法运行这些异步。使这项工作更为复杂的实际代码,特别是在错误处理方面:/btw,为了将来的参考,这是我对代码所做的相关承诺。值得注意的是,每个并行任务必须接受并调用一个回调函数来指示它已经完成。使这项工作正常进行的实际代码要复杂得多,特别是在错误处理方面:/btw,作为将来的参考,这是我对代码所做的相关承诺。值得注意的是,每个并行任务必须接受并调用一个回调函数来指示它已经完成。这很有趣。我不知道Promisify,也不知道当它全部完成时代码会有多优雅promises@Earlz是的,将结果作为正态变量的值是非常有趣的。我不知道Promisify,也不知道当它全部完成时代码会有多优雅promises@Earlz是的,将结果作为正态变量中的值是非常棒的