由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教一下:Javascript callback not working
相关主题
node promise发出reject之后,是不是只能hit catch block ?说了半天异步是大势所趋没什么疑问了
学了这么多语言发现还是coffeescript最好用asynchronous vs non-blocking
用node怎么把多个mysql query 的结果合起来看了一下C#的async await
哈哈, 不喜欢typescript 是不是都是新手?看看大牛们为什么都远离.net
node 求算法这年头async IO成了流行了
如何定义 Javascript overload function ?请求推荐javascript程序做scrolling
同步编程真郁闷不要小看js
尼玛 callback 真是反人类多线程,异步,并发冲突,fp和其它
相关话题的讨论汇总
话题: callback话题: err话题: function话题: q1话题: var
进入Programming版参与讨论
1 (共1页)
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.

相关主题
如何定义 Javascript overload function ?说了半天异步是大势所趋没什么疑问了
同步编程真郁闷asynchronous vs non-blocking
尼玛 callback 真是反人类看了一下C#的async await
进入Programming版参与讨论
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, 糙猛快,:)
相关主题
看看大牛们为什么都远离.net不要小看js
这年头async IO成了流行了多线程,异步,并发冲突,fp和其它
请求推荐javascript程序做scrolling真正对异步有需求的应该是游戏类服务器
进入Programming版参与讨论
l**********n
发帖数: 8443
21
callback是个设计的问题,设计得不好,有时难用,但不等于callback本身有什么问题
。java连closure都没有,只能用各种design pattern来修补,但不等于java这个语言
就是完美了。C#比java强多了,有closure,有property。
l**********n
发帖数: 8443
22
java的多线程才难用,callback有什么难用的。
l**********n
发帖数: 8443
23
callback是fp的精髓。
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。

1 (共1页)
进入Programming版参与讨论
相关主题
多线程,异步,并发冲突,fp和其它node 求算法
真正对异步有需求的应该是游戏类服务器如何定义 Javascript overload function ?
node.js child process: 怎样保证1个命令执行完了再执行下一个?同步编程真郁闷
问个 rxjava 的问题尼玛 callback 真是反人类
node promise发出reject之后,是不是只能hit catch block ?说了半天异步是大势所趋没什么疑问了
学了这么多语言发现还是coffeescript最好用asynchronous vs non-blocking
用node怎么把多个mysql query 的结果合起来看了一下C#的async await
哈哈, 不喜欢typescript 是不是都是新手?看看大牛们为什么都远离.net
相关话题的讨论汇总
话题: callback话题: err话题: function话题: q1话题: var