w*s 发帖数: 7227 | 1 打开页面localhost:8080/db1, 页面停滞不动。
server端打印出step 1, step 2, 但没有step 3.
请问为什么?谢谢!
主程序main.js
const express = require('express')
const app = express()
const port = 8080
var db1 = require("./db.js");
app.get('/', (request, response) => {
response.send('Hello from Express!')
})
app.get('/db1', (request, response) => {
var q1 = db1.query1(function(){
console.log("step 3: " + q1);
response.send('Hello from db1! ' + q1);
});
})
app.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
})
第二个文件db.js
var mysql = require('mysql');
var async = require('async');
var connection = mysql.createConnection({
host : 'localhost',
user : 'admin',
password : 'password',
database : 'mydb'
});
var query1 = function() {
var q1 = "NA";
async.series([
function(callback) {
connection.connect();
connection.query('SELECT * from customer;',
function(err, rows, fields) {
if (err){
console.log("---- Not supposed to see this -----");
callback(true);
}
q1 = rows[0].name;
console.log('step 1: ', rows[0].name);
callback(null, q1);
});
},
],
function(err, result) {
connection.end();
if(err) {
return "NA";
} else {
console.log("step 2: ", result);
return result[0].name;
}
}
);
};
module.exports = {
query1: query1
}; |
c*********e 发帖数: 16335 | 2 明天要给老板汇报了?
【在 w*s 的大作中提到】 : 打开页面localhost:8080/db1, 页面停滞不动。 : server端打印出step 1, step 2, 但没有step 3. : 请问为什么?谢谢! : 主程序main.js : const express = require('express') : const app = express() : const port = 8080 : var db1 = require("./db.js"); : app.get('/', (request, response) => { : response.send('Hello from Express!')
|
w*s 发帖数: 7227 | 3 kao, 求答案
【在 c*********e 的大作中提到】 : 明天要给老板汇报了?
|
l**********n 发帖数: 8443 | 4 你这个写得一踏糊涂。不及格。你的query1并没有accept callback,所以callback
never called。我要是老师给评分的话,给0分。 |
c*********e 发帖数: 16335 | 5 我比较仁慈,给1分。
【在 l**********n 的大作中提到】 : 你这个写得一踏糊涂。不及格。你的query1并没有accept callback,所以callback : never called。我要是老师给评分的话,给0分。
|
l**********n 发帖数: 8443 | 6 query1 should be:
function(callback){
async.series({
one: function(callback){
setTimeout(function(){
callback(null, 1);
}, 200);
},
two: function(callback){
setTimeout(function(){
callback(null, 2);
}, 100);
}
},
function(err, results) {
callback(); // invoke the callback here according to the results or err
});
} |
s*i 发帖数: 5025 | 7 This is what I am reading from your code:
1. connection.connect()
2. connection.query(....)
//Remember, connection.query(...) takes time and before it even does
anything, you called:
3. connection.close()
I am surprised that it printed out 'step 1'. It should only print 'step 2'.
【在 w*s 的大作中提到】 : 打开页面localhost:8080/db1, 页面停滞不动。 : server端打印出step 1, step 2, 但没有step 3. : 请问为什么?谢谢! : 主程序main.js : const express = require('express') : const app = express() : const port = 8080 : var db1 = require("./db.js"); : app.get('/', (request, response) => { : response.send('Hello from Express!')
|
w*s 发帖数: 7227 | 8 i just barely made it work.
it's very confusing, async takes callback.
Then there's a callback from db1.query1(callback),
the names are the same, very confusing.
【在 l**********n 的大作中提到】 : query1 should be: : function(callback){ : async.series({ : one: function(callback){ : setTimeout(function(){ : callback(null, 1); : }, 200); : }, : two: function(callback){ : setTimeout(function(){
|
s*i 发帖数: 5025 | 9 name callbacks differently? like queryCallback?
【在 w*s 的大作中提到】 : i just barely made it work. : it's very confusing, async takes callback. : Then there's a callback from db1.query1(callback), : the names are the same, very confusing.
|
c*********e 发帖数: 16335 | 10 你自己写的代码?
【在 w*s 的大作中提到】 : i just barely made it work. : it's very confusing, async takes callback. : Then there's a callback from db1.query1(callback), : the names are the same, very confusing.
|
|
|
q*c 发帖数: 9453 | 11 开始知道 callback hell 的威力啦?
这才是毛毛雨。 时间长了十层八层混在一起都是常事,而且没类型。
这就是为啥 stick with old wisdom 是真理。
【在 w*s 的大作中提到】 : i just barely made it work. : it's very confusing, async takes callback. : Then there's a callback from db1.query1(callback), : the names are the same, very confusing.
|
w*s 发帖数: 7227 | 12 然
【在 c*********e 的大作中提到】 : 你自己写的代码?
|
w*s 发帖数: 7227 | 13 尼玛,别吓老子
【在 q*c 的大作中提到】 : 开始知道 callback hell 的威力啦? : 这才是毛毛雨。 时间长了十层八层混在一起都是常事,而且没类型。 : 这就是为啥 stick with old wisdom 是真理。
|
c*********e 发帖数: 16335 | 14 自己写的代码,自己都看不懂?
“the names are the same, very confusing”????
你自己写的代码?
【在 w*s 的大作中提到】 : 然
|
w*s 发帖数: 7227 | 15 说过啦,我是写C出身,没有正规学习过js,就是工作中边做边学,赶进度,半懂不懂
之间应付过去。
你现在歧视我,等你老了也会有被人歧视的一天。
【在 c*********e 的大作中提到】 : 自己写的代码,自己都看不懂? : “the names are the same, very confusing”???? : : 你自己写的代码?
|
l*********s 发帖数: 5409 | 16 很好奇怎么找到前端工作的?介绍下面试技巧?
【在 w*s 的大作中提到】 : 说过啦,我是写C出身,没有正规学习过js,就是工作中边做边学,赶进度,半懂不懂 : 之间应付过去。 : 你现在歧视我,等你老了也会有被人歧视的一天。
|
w*s 发帖数: 7227 | 17 没有,就是在组里面转的,因为产品一直需要网站的,其实我一半是做node.js等
【在 l*********s 的大作中提到】 : 很好奇怎么找到前端工作的?介绍下面试技巧?
|
n****j 发帖数: 1708 | 18 还有一个严重问题,if err callback 之后程序并没返回,会继续执行
【在 l**********n 的大作中提到】 : 你这个写得一踏糊涂。不及格。你的query1并没有accept callback,所以callback : never called。我要是老师给评分的话,给0分。
|
w*s 发帖数: 7227 | 19 agile development, 糙猛快,:)
【在 n****j 的大作中提到】 : 还有一个严重问题,if err callback 之后程序并没返回,会继续执行
|
c*********e 发帖数: 16335 | 20 有人做test?如果没有,那就是哈哈了。
【在 w*s 的大作中提到】 : agile development, 糙猛快,:)
|
|
|
l**********n 发帖数: 8443 | 21 callback是个设计的问题,设计得不好,有时难用,但不等于callback本身有什么问题
。java连closure都没有,只能用各种design pattern来修补,但不等于java这个语言
就是完美了。C#比java强多了,有closure,有property。 |
l**********n 发帖数: 8443 | 22 java的多线程才难用,callback有什么难用的。 |
l**********n 发帖数: 8443 | |
w*s 发帖数: 7227 | 24 还有人做test,你真搞笑了。具体的还真没法在网上跟你说了。
【在 c*********e 的大作中提到】 : 有人做test?如果没有,那就是哈哈了。
|
c*********e 发帖数: 16335 | 25 C#比java强多了? 这也是醉了。
【在 l**********n 的大作中提到】 : callback是个设计的问题,设计得不好,有时难用,但不等于callback本身有什么问题 : 。java连closure都没有,只能用各种design pattern来修补,但不等于java这个语言 : 就是完美了。C#比java强多了,有closure,有property。
|